绑定DataGrid列的能见度MVVM [英] Bind datagrid column visibility 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
。
DataGridColumn
s 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...
- 添加一个代理
FrameworkElement的
在你的祖先面板的资源
。 - 主机它变成一种无形的
ContentControl中
绑定到其内容
。 -
使用该
ProxyElement
为的StaticResource
在您的能见度数据上下文源绑定。
- 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.
<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 Popup
s and ContextMenu
s (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) ...
-
变化框架元素的资源,就像一个
文本块
轻便。 (Silverlight不允许指定FrameworkElement的
类型的静态资源。)
Change the framework element resource to something lightweight like a
Textblock
. (Silverlight does not allow specifying static resource ofFrameworkElement
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屋!