Using a ValueConverter after a DataContext switch

I want to try out a small, custom ValueConverter in a class which after being instantiated (via default constructor, which only calls InitializeComponents() ), is given another DataContext, specifically an instance of a ViewModel.

Using a StaticResource within the Binding does not work at all (yields a NullReferenceException), since the DataContext has since then been changed (is not this anymore).

I've tried putting DataContext = this; before the InitializeComponents call, no change. Should I be looking into this MarkupExtension gizmo (as described in this article) ?

I've also tried creating an instance of the custom Value Converter within the ViewModel (the current DataContext), doesn't help either.

I can provide additional details at all times. Thank you in advance !

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.

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 my other question.

Edit :

  <Helpers:DisplayTypeToDefaultValueConverter x:Key="displayTypeConverter" />

<TextBlock x:Name="InstructionLabel" 
           TextWrapping="Wrap" Text="{Binding Path=SelectedNodeText}" 

      <MenuItem Header="Settings" Name="SettingsPop" 
                ItemsSource="{Binding Source={StaticResource DisplayTypeValues}}"

          <Style TargetType="MenuItem">
            <Setter Property="Header" Value="{Binding}"/>
            <Setter Property="IsChecked">
                <Binding Converter="{StaticResource displayTypeConverter}" />


I've only now realized that it's the dreaded ContextMenu. That's the problem, isn't it ?


The DataContext inside the ItemContainerStyle is a member of the DisplayTypeValues collection. The only thing in the XAML you posted that will be affected by the DataContext of the UserControl changing is the InstructionLabel's text. Setting DataContext="{Binding}" as you're doing on the MenuItem is also redundant as the value will already be inherited from the parent ContextMenu.

It's not clear from your question or code what you're expecting from the DataContext or what you're trying to do with it.

Need Your Help

Grouping table by only one field with linq

c# sql sql-server linq

I have table with 7 fields in MSSQL server MDF file that imports CSV files and need to generate few reports for them.

How can I send an AWS SQS message with C# with a custom timeout?

c# amazon-sqs

Here's my basic code to send an SQS message from C# using the AWS .NET SDK. How can I give the message a different timeout other than the queue default?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.