本文介绍了WPF的DataGrid:绑定的DataGridColumn知名度的ContextMenu的MenuItems器isChecked(MVVM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个文本菜单可用于控制的DataGrid 列知名度,用户右键单击列标题。在文本菜单显示所有可用列的名称。我使用MVVM设计模式。

I want to control DataGrid column visibility through a ContextMenu available to the user by right-clicking the column header. The ContextMenu displays the names of all available columns. I am using MVVM design pattern.

我的问题是:我如何绑定的DataGridColumn 能见度属性设置为器isChecked 的属性菜单项位于文本菜单

My question is: How do I bind the DataGridColumn's Visibility property to the IsChecked property of a MenuItem located in the ContextMenu.

一些样机code:

<UserControl.Resources>         
    <ContextMenu x:Key="ColumnHeaderContextMenu">  
        <MenuItem Header="Menu Item..1" IsCheckable="True" />  
    </ContextMenu>  
    <Style x:Key="ColumnHeaderStyle" 
           TargetType="{x:Type toolkit:DataGridColumnHeader}">  
        <Setter Property="ContextMenu" 
                Value="{StaticResource ColumnHeaderContextMenu}" />  
    </Style>  
    <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />  
</UserControl.Resources>  

... FLAF FLAF FLAF

...flaf flaf flaf

<toolkit:DataGrid x:Name="MyGrid" AutoGenerateColumns="False" 
    ItemsSource="{Binding MyCollection, Mode=Default}" 
    EnableColumnVirtualization="True" IsReadOnly="True" 
    ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}">  
    <toolkit:DataGrid.Columns>  
        <toolkit:DataGridTextColumn Binding="{Binding Path=MyEntry}" 
            Header="MyEntry" Visibility="{Binding IsChecked, Converter=
                {StaticResource booleanToVisibilityConverter}.... />
    </toolkit:DataGrid.Columns>     
</toolkit:DataGrid>  

如果我是不清楚,请让我知道,我将试图阐述。

If I am being unclear please let me know and I will attempt to elaborate.

干杯,

推荐答案

我刚写了一篇关于这一主题的博客文章。它允许显示或通过文本菜单,通过右键单击任何列标题是可访问的隐藏DataGridColumns。这个任务是通过附加属性纯粹的完成,因此是MVVM兼容。

I just wrote a blog post on this topic. It allows DataGridColumns to be shown or hidden through a ContextMenu that is accessible by right-clicking any column header. This task is accomplished purely through attached properties so it is MVVM-compliant.

<一个href=\"http://thrash505.word$p$pss.com/2011/01/11/showhide-datagridcolumns-using-a-contextmenu-on-microsofts-wpf-datagrid-with-attached-properties/#more-254\">See博客文章

这篇关于WPF的DataGrid:绑定的DataGridColumn知名度的ContextMenu的MenuItems器isChecked(MVVM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 16:51