Binding visibility to the checked menu item shows the error "The service provider does not have the INameResolver service" in WPF

I am trying to show / hide datagrid columns via the context menu. I tried to use bindings for it using this XAML:

<Grid> <DataGrid AutoGenerateColumns="False" Name="dataGrid1"> <DataGrid.ContextMenu> <ContextMenu> <MenuItem Header="Show Column 1" IsCheckable="True" x:Name="showcol1" IsChecked="True" /> <MenuItem Header="Show Column 2" IsCheckable="True" x:Name="showcol2" IsChecked="False" /> </ContextMenu> </DataGrid.ContextMenu> <DataGrid.Columns> <DataGridTextColumn Header="Col 0" /> <DataGridTextColumn Header="Col 1" Visibility="{Binding ElementName=showcol1, Converter={StaticResource BooleanToVisibilityConverter}, Path=IsChecked}" /> <DataGridTextColumn Header="Col 2" Visibility="{Binding ElementName=showcol2, Converter={StaticResource BooleanToVisibilityConverter}, Path=IsChecked}" /> </DataGrid.Columns> </DataGrid> </Grid> 

I even experimented with other parameters like BindsDirectlyToSource=True and UpdateSourceTrigger=PropertyChanged . However, the columns do not change the visibility when I check / uncheck the boxes from the menu items. What am I doing wrong? Is this possible in pure XAML?

The x:Reference answers this question. I tried too but got an error

The service provider does not have the INameResolver service.

Google told me this is a bug in VS2010? What can I do to solve this problem? Or is my best shot to switch to VS2012?

+7
source share
1 answer

Here is an explanation from Adam Nathan WPF 4 unleashed book (I advise everyone to read):

The x: Reference markup extension is often mistakenly associated with XAML2009 functions that can only be used from free XAML at the time of this writing. Although x: Reference is a new feature in WPF 4, it can be used with XAML2006 just fine, as long as your project targets version 4 or later of the .NET Framework. One failure is that the XAML designer in Visual Studio 2010 does not correctly handle x: Reference, so it gives the following development-time error, which you can safely ignore: The service provider does not have the INameResolver service.

In any case, this message can be ignored. For my Visual Studio 2010 it sometimes appears, sometimes not.

EDIT:

I found another quote ( source ), but they do not offer specific solutions:

When using {x: Reference} as the target of the WPF shortcut, the Visual Studio designer throws an InvalidOperationException with the message "The service provider does not have the INameResolver service." The project will compile and run without any problems, but the Design canvas, which displays x: Reference, will be disabled due to an exception. At the time of this writing, this is a known issue and needs to be addressed in the future.

Here, the author specifically explains this problem and writes that he sent a bug report to Microsoft .

BooleanToVisibilityConverter

 <Window.Resources> <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> </Window.Resources> 

DataGrid XAML

 <DataGrid AutoGenerateColumns="False" Name="dataGrid1"> <DataGrid.ContextMenu> <ContextMenu> <MenuItem x:Name="showcol1" Header="Show Column 1" IsCheckable="True" IsChecked="True" /> <MenuItem x:Name="showcol2" Header="Show Column 2" IsCheckable="True" IsChecked="False" /> </ContextMenu> </DataGrid.ContextMenu> <DataGrid.Columns> <DataGridTextColumn Header="Col 0" /> <DataGridTextColumn Header="Col 1" Visibility="{Binding Source={x:Reference Name=showcol1}, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}" /> <DataGridTextColumn Header="Col 2" Visibility="{Binding Source={x:Reference Name=showcol2}, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}" /> </DataGrid.Columns> </DataGrid> 
+4
source

All Articles