7

I am trying to show/hide columns of a datagrid via a context menu. I was trying to use bindings for it, with 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 the other options, such as BindsDirectlyToSource=True and UpdateSourceTrigger=PropertyChanged. However, the columns do not change their visibility when I check/uncheck the menuitems. What am I doing wrong? Is this actually possible in pure XAML?

In this question, the answer uses x:Reference. I tried that too but received the error

Service provider is missing the INameResolver service.

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

Community
  • 1
  • 1
CBenni
  • 555
  • 1
  • 7
  • 20

1 Answers1

5

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

The x:Reference markup extension is often mistakenly associated with the XAML2009 features that can only be used from loose XAML at the time of this writing. Although x:Reference is a new feature in WPF 4, it can be used from XAML2006 just fine as long as your project is targeting version 4 or later of the .NET Framework. One glitch is that the XAML designer in Visual Studio 2010 doesn't properly handle x:Reference, so it gives the following design-time error that you can safely ignore: Service provider is missing the INameResolver service.

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

EDIT:

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

When using {x: Reference } as the Target of a WPF Label, the Visual Studio designer throws an InvalidOperationException exception with the message "Service provider is missing the INameResolver service." The project will compile and execute without any issues, but the Design canvas where the x: Reference appears will be disabled because of the exception. As of this book's writing, this is a known issue and should be resolved sometime in the future.

Here, author specifically explains the problem, and writes that sent the 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>    
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
  • That book is currently being shipped here. I knew I should have waited until it arrives ;) thank you! I had the issue that the designer refuded to Work as long as the issue isnt resolved - can you confirm this and maybe give a way to fix that? – CBenni Jun 30 '13 at 16:53
  • I do not think that this is a `serious` problem. `Microsoft` does not correct the error and worse :). If I'm not mistaken, they did not even recognize it for error, so fix can wait a long time. I personally use a `x:Reference` in their projects and problems are not observed. – Anatoliy Nikolaev Jun 30 '13 at 17:00
  • 1
    It shows the message "InvalidOperationException was thrown on 'Reference': Service provider is missing the INameResolver service. Click here for detail." in the designer tab. I cant use the designer at all, because it is disabled. I will remove the reference until I finish the project – CBenni Jun 30 '13 at 17:50