Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a ValueConverter after a DataContext switch
    primarykey
    data
    text
    <p>I want to try out a small, custom <code>ValueConverter</code> in a class which after being instantiated (via default constructor, which only calls <code>InitializeComponents()</code> ), is given another <code>DataContext</code>, specifically an instance of a ViewModel.</p> <p>Using a <code>StaticResource</code> within the <code>Binding</code> does not work at all (yields a <code>NullReferenceException</code>), since the <code>DataContext</code> has since then been changed (is not <code>this</code> anymore). </p> <p>I've tried putting <code>DataContext = this;</code> before the <code>InitializeComponents</code> call, no change. Should I be looking into this <code>MarkupExtension</code> gizmo (as described <a href="http://www.codeproject.com/KB/WPF/MarkupExtensionsConverter.aspx" rel="nofollow noreferrer">in this article</a>) ?</p> <p>I've also tried creating an instance of the custom <code>Value Converter</code> within the ViewModel (the current <code>DataContext</code>), doesn't help either.</p> <p>I can provide additional details at all times. Thank you in advance !</p> <p>I'm trying to display a ContextMenu within the TextBlock. The ContextMenu contains a sole MenuItem. The header of the MenuItem can be "Settings" for instance. The Children (rendered as MenuItems as well) of the said MenuItem stem from an Enum, hence the ItemsSource on the MenuItem.</p> <p>Now all is getting displayed nicely, yet I am trying to make one of the Children (e.g. a member of the Enum) to be selected per default, since there is already a default Setting. Further background info can be found in <a href="https://stackoverflow.com/questions/4305439/how-to-set-a-menuitems-ischecked-property-for-a-child-of-type-enum-via-xaml">my other question</a>.</p> <p><strong>Edit</strong> :</p> <pre><code>... &lt;UserControl.Resources&gt; &lt;Helpers:DisplayTypeToDefaultValueConverter x:Key="displayTypeConverter" /&gt; &lt;/UserControl.Resources&gt; ... &lt;TextBlock x:Name="InstructionLabel" TextWrapping="Wrap" Text="{Binding Path=SelectedNodeText}" Grid.RowSpan="1"&gt; &lt;TextBlock.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="Settings" Name="SettingsPop" DataContext="{Binding}" ItemsSource="{Binding Source={StaticResource DisplayTypeValues}}" IsCheckable="True" Click="SettingsType_Click"&gt; &lt;MenuItem.ItemContainerStyle&gt; &lt;Style TargetType="MenuItem"&gt; &lt;Setter Property="Header" Value="{Binding}"/&gt; &lt;Setter Property="IsChecked"&gt; &lt;Setter.Value&gt; &lt;Binding Converter="{StaticResource displayTypeConverter}" /&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/MenuItem.ItemContainerStyle&gt; &lt;/ContextMenu&gt; &lt;/TextBlock&gt; </code></pre> <p>I've only now realized that it's the dreaded ContextMenu. That's the problem, isn't it ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload