绑定DataGrid列的能见度MVVM [英] Bind datagrid column visibility MVVM

查看:188
本文介绍了绑定DataGrid列的能见度MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

净3.5

我知道,列不继承的DataContext和阅读其他帖子我认为这会工作:

I know that the columns doesn't inherit the datacontext and by reading other posts i thought this would work:

Visibility="{Binding RelativeSource={x:Static RelativeSource.Self},
                     Path=(FrameworkElement.DataContext).IsColumnNameVisible,
                     Converter={StaticResource boolToVisConverter}}"

不过,当然它不.. 输出窗口不抱怨,似乎资源,我发现,但视图模型属性为新的调用。

However of course it doesn't.. The output window does not complain, it seems that the resource i found but the viewmodel property is newer called.

这是整个DG:

<tk:DataGrid                                        
            VirtualizingStackPanel.IsVirtualizing="False"                                        
            Grid.Column="0"
            AlternationCount="2"
            AreRowDetailsFrozen="True"
            AutoGenerateColumns="False"
            Background="Transparent"
            BorderThickness="0"
            CanUserAddRows="False"
            CanUserReorderColumns="True"
            CanUserResizeRows="False"
            GridLinesVisibility="None"
            ItemsSource="{Binding Employees}"
            SelectionMode="Single"
            ColumnHeaderStyle="{StaticResource columnHeaderStyle}"
            RowHeaderStyle="{StaticResource rowHeaderStyle}"
            CellStyle="{StaticResource cellStyle}"
            RowStyle="{StaticResource rowStyle}" 
            ContextMenu="{StaticResource columnHeaderContextMenu}">
    <tk:DataGrid.Resources>
        <ContextMenu x:Key="columnHeaderContextMenu" ItemsSource="{Binding ColumnHeaderContextMenuItems}" />
        <Style TargetType="{x:Type ScrollBar}">
            <Setter Property="Background" Value="Transparent"/>
        </Style>                                    
        <Style TargetType="{x:Type tk:DataGridColumnHeader}">
            <Setter Property="Background" Value="Transparent"/>
        </Style>
    </tk:DataGrid.Resources>
    <tk:DataGrid.Triggers>
        <EventTrigger RoutedEvent="tk:DataGridRow.MouseDoubleClick">
            <EventTrigger.Actions>
                <BeginStoryboard Storyboard="{StaticResource showDetailGrid}"/>
            </EventTrigger.Actions>
        </EventTrigger>
    </tk:DataGrid.Triggers>
    <tk:DataGrid.Columns>
        <tk:DataGridTextColumn IsReadOnly="True" Header="test" Binding="{Binding Name, Mode=OneWay}" Visibility="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(FrameworkElement.DataContext).IsColumnNameVisible, Converter={StaticResource boolToVisConverter}}"  />
    </tk:DataGrid.Columns>
</tk:DataGrid>

我看了pretty的多少每一个解决了这个问题,并没有什么作品。

I have read pretty much every single solution to this problem and nothing works..

推荐答案

的DataGridColumn 是不可视树的一部分,因此它们没有连接到的数据上下文的的DataGrid

DataGridColumns are not part of visual tree so they are not connected to the data context of the DataGrid.

有关他们的连接一起使用代理元素的方法是这样...

For them to connect together use proxy element approach like this...

  1. 添加一个代理 FrameworkElement的在你的祖先面板的资源
  2. 主机它变成一种无形的 ContentControl中绑定到其内容
  3. 使用该 ProxyElement 的StaticResource 在您的能见度数据上下文源绑定。

  1. Add a proxy FrameworkElement in your ancestor panel's Resources.
  2. Host it into an invisible ContentControl bound to its Content.
  3. Use this ProxyElement as StaticResource for data context source in your visibility binding.

<StackPanel>
    <StackPanel.Resources>
       <local:BooleanToVisibilityConverter
              x:Key="BooleanToVisibilityConverter" />

       <FrameworkElement x:Key="ProxyElement"
                         DataContext="{Binding}"/>
    </StackPanel.Resources>
    <ContentControl Visibility="Collapsed"
                Content="{StaticResource ProxyElement}"/>
    <DataGrid AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn
                   Visibility="{Binding DataContext.IsTextColumnVisibile,
                                        Source={StaticResource ProxyElement},
                                        Converter={StaticResource
                                            BooleanToVisibilityConverter}}"
                   Binding="{Binding Text}"/>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel> 

除了的DataGridColumn ,上述办法还的伟大工程来连接的DataContext 弹出文本菜单 S(即未连接到视觉树中的任何元素)。

Apart from DataGridColumn, the above approach also works great to connect DataContext to Popups and ContextMenus (i.e. any element that is not connected to the visual tree).

的Silverlight用户

与任何框架元素含量的控制可悲的是设定内容是不是在Silverlight允许的。因此,解决方法是(这只是一个指导code为Silverlight)...

Sadly setting contents of content controls with any framework elements is not allowed in silverlight. So the workaround would be (this is just a guidance code for silverlight) ...

  1. 变化框架元素的资源,就像一个文本块轻便。 (Silverlight不允许指定 FrameworkElement的类型的静态资源。)

  1. Change the framework element resource to something lightweight like a Textblock. (Silverlight does not allow specifying static resource of FrameworkElement type.)

<StackPanel.Resources>
    <TextBlock x:Key="MyTextBlock" />

  • 写一个附加的属性来保存文本块对内容的控制。

  • Write an attached property to hold text block against the content control.

    <ContentControl Visibility="Collapsed" 
                    local:MyAttachedBehavior.ProxyElement="{StaticResource MyTextBlock}" />
    

  • 在附加依赖属性更改事件处理程序,设置绑定的内容控件的数据上下文的文本块的。

  • In the attached dependency property changed event handler, set the bind the data context of the content control to the text block's.

     private static void OnProxyElementPropertyChanged(
         DependencyObject depObj, DependencyPropertyChangedEventArgs e)
     {
           if (depObj is ContentControl && e.NewValue is TextBlock)
           {
               var binding = new Binding("DataContext");
               binding.Source = depObj;
               binding.Mode = OneWay;
               BindingOperations.SetBinding(
                   (TextBlock)e.NewValue, TextBlock.DataContextProperty, binding);
           }
     }
    

  • 那么这样的文本块可能无法连接到可视化树而将可能了解数据方面的变化。

    So this way the textblock may not be connected to the visual tree but will probably be aware of the data context changes.

    希望这有助于。

    这篇关于绑定DataGrid列的能见度MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆