绑定数据网格列可见性MVVM
.Net 3.5
我知道列不会继承数据上下文,通过阅读其他帖子,我认为这会起作用:
Visibility="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(FrameworkElement.DataContext).IsColumnNameVisible,
Converter={StaticResource boolToVisConverter}}"
但是当然它不会.. 输出窗口没有抱怨,似乎我找到的资源但 viewmodel 属性是更新调用的。
这是整个 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>
我已经阅读了这个问题的几乎所有解决方案,但没有任何效果。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DataGridColumn
不是可视化树的一部分,因此它们不连接到DataGrid
的数据上下文。为了让它们连接在一起,请使用像这样的代理元素方法...
在祖先面板的
Resources
中添加代理FrameworkElement
。将其托管到与其
Content
绑定的不可见ContentControl
中。将此
ProxyElement
用作可见性绑定中数据上下文源的StaticResource
。除了
DataGridColumn
之外,上述方法还可以很好地将DataContext
连接到Popup
和ContextMenu
(即任何未连接到可视树的元素)。Silverlight 用户
遗憾的是,silverlight 中不允许使用任何框架元素设置内容控件的内容。因此,解决方法是(这只是 silverlight 的指导代码)...
将框架元素资源更改为轻量级的资源,例如
Textblock
。 (Silverlight 不允许指定FrameworkElement
类型的静态资源。)编写附加属性以针对内容控件保存文本块。
在附加的依赖属性更改事件处理程序中,将内容控件的数据上下文绑定到文本块。
因此,通过这种方式,文本块可能不会连接到可视化树,但可能会意识到数据上下文的更改。
DataGridColumn
s are not part of visual tree so they are not connected to the data context of theDataGrid
.For them to connect together use proxy element approach like this...
Add a proxy
FrameworkElement
in your ancestor panel'sResources
.Host it into an invisible
ContentControl
bound to itsContent
.Use this
ProxyElement
asStaticResource
for data context source in your visibility binding.Apart from
DataGridColumn
, the above approach also works great to connectDataContext
toPopup
s andContextMenu
s (i.e. any element that is not connected to the visual tree).Silverlight Users
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) ...
Change the framework element resource to something lightweight like a
Textblock
. (Silverlight does not allow specifying static resource ofFrameworkElement
type.)Write an attached property to hold text block against the content control.
In the attached dependency property changed event handler, set the bind the data context of the content control to the text block's.
So this way the textblock may not be connected to the visual tree but will probably be aware of the data context changes.