[No0000123]WPF DataGrid Columns Visibility的绑定
场景:根据配置文件显示DataGrid中的某些列。
问题:Columns集合只是DataGrid的一个属性,这个集合在逻辑树或视觉树中是看不到的,也不会继承DataContext属性。
方法一:对DataGridColumn附加DataContext属性
该方法需要用到一个帮助类(需要创建一个全局实例),具体内容如下:
public class DataGridContextHelper
{
static DataGridContextHelper()
{ DependencyProperty dp = FrameworkElement.DataContextProperty.AddOwner(typeof(DataGridColumn));
FrameworkElement.DataContextProperty.OverrideMetadata(typeof(DataGrid),
new FrameworkPropertyMetadata
(null, FrameworkPropertyMetadataOptions.Inherits,
new PropertyChangedCallback(OnDataContextChanged))); } public static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DataGrid grid = d as DataGrid;
if (grid != null)
{
foreach (DataGridColumn col in grid.Columns)
{
col.SetValue(FrameworkElement.DataContextProperty, e.NewValue);
}
}
}
}
前台绑定示例
<DataGridTextColumn x:Name="col2" Header="TestColumn"
Visibility="{Binding (FrameworkElement.DataContext).Show,
RelativeSource={RelativeSource Self},
Converter={StaticResource cc}}"></DataGridTextColumn>
后台绑定示例
var binding = new Binding();
binding.Mode = BindingMode.OneWay;
binding.RelativeSource=new RelativeSource(RelativeSourceMode.Self);
binding.Converter = new BooleanToVisibilityConverter();
binding.Path=new PropertyPath("(FrameworkElement.DataContext).Show");
BindingOperations.SetBinding(obj, DataGridColumn.VisibilityProperty, binding);
方法二:通过Source直接与Vm绑定
示例代码:
var binding = new Binding();
binding.Mode = BindingMode.OneWay;
binding.Source = vm;
binding.Converter = new BooleanToVisibilityConverter();
binding.Path = new PropertyPath("Show");
BindingOperations.SetBinding(obj, DataGridColumn.VisibilityProperty, binding);
方法三:通过Source与VM的一个代理类进行绑定
代理类
Freezable可以继承DataContext即使它们不在视觉树或逻辑树中
public class BindingProxy : Freezable
{
#region Overrides of Freezable protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
} #endregion public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
} public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object),
typeof(BindingProxy));
}
前台绑定代码
<DataGrid x:Name="dg" HorizontalAlignment="Left" VerticalAlignment="Top" Height="253" Width="436" ItemsSource="{Binding Persons}"
AutoGenerateColumns="False">
<DataGrid.Resources>
<dgTest:BindingProxy x:Key="proxy" Data="{Binding}"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn x:Name="col1" Header="TestColumn" Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTextColumn x:Name="col2" Header="TestColumn"
Visibility="{Binding (FrameworkElement.DataContext).Show,
RelativeSource={RelativeSource Self},
Converter={StaticResource cc}}"></DataGridTextColumn>
<DataGridTextColumn x:Name="col3" Header="TestColumn"
Visibility="{Binding Data.Show,Source={StaticResource proxy},
Converter={StaticResource cc}}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
方法四:通过一个代理控件来实现
代理控件与代理对象的解决方式类似,都是因为其可以继承DataContext属性,只是一个在逻辑树中看的到,一个看不到。示例如下:
<FrameworkElement x:Name="dummyElement" Visibility="Collapsed"/>
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Test"
Binding="{Binding Name}"
Visibility="{Binding DataContext.IsEnable,
Source={x:Reference dummyElement}}"/>
</DataGrid.Columns>
</DataGrid>
[No0000123]WPF DataGrid Columns Visibility的绑定的更多相关文章
- WPF DATAGrid 空白列 后台绑定列 处理
原文:WPF DATAGrid 空白列 后台绑定列 处理 AutoGenerateColumns <DataGrid x:Name="dataGrid" Margin=&qu ...
- WPF DataGrid 双击行 获得绑定数据
原文:WPF DataGrid 双击行 获得绑定数据 1)增加事件 2)增加对象获取 1)事件代码 Datagrid 增加事件 MouseDoubleClick="dataGrid_Mous ...
- 【WPF】WPF DataGrid List数据源 双向绑定通知机制之ObservableCollection使用以及MultiBinding 的应用
以下代码实现了DataGrid的简单绑定List数据源 重点要提一下的是,绑定List数据源,但是不能直接用List.比如下面的代码,使用List<GridItem>只能实现数据修改的绑定 ...
- WPF DataGrid、ListView 简单绑定
DataGrid运行效果: xaml 代码: DataGridName= dtgData ItemsSource= {Binding} AutoGenerateColumns= False DataG ...
- WPF Datagrid 动态生成列 并绑定数据
原文:WPF Datagrid 动态生成列 并绑定数据 说的是这里 因为列头是动态加载的 (后台for循环 一会能看到代码) 数据来源于左侧列 左侧列数据源 当然num1 属于临时的dome使用 可 ...
- WPF DataGrid 绑定数据及时更新的处理
原文:WPF DataGrid 绑定数据及时更新的处理 默认情况下datagrid 绑定数据源后,在界面编辑某一列后,数据不会及时更新到内存对象中.如在同一行上有一个命令对来获取 当前选中行(内存对象 ...
- WPF DataGrid绑定及列居中
基本的数据绑定 把集合的字段(属性)绑定在DataGrid的Binding属性就能将数据绑定列表 public class CashItem { public int Value { get; set ...
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.
WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次 悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...
- WPF DataGrid 绑定DataSet数据 自动生成行号
1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...
随机推荐
- 12C -- ORA-65048 ORA-65048
创建common user的时候报错: $ sqlplus '/as sysdba' SQL*Plus: Release 12.2.0.1.0 Production on Tue Apr 18 11: ...
- appium 获取android 粘贴板上的内容
appium 新版本增加了获取粘贴板的内容.如果使用appium旧版本,获取粘贴板的内容不是那么容易的,甚至百度谷歌各种搜,都无法找到合适的解决方法.新版本获取android 粘贴板内容就显得很容易了 ...
- 三星S4 i9508 4.4.2 root 教程
现在有很多一键root的软件,但是各怀鬼胎,最好不用. 最近玩腻了三方的rom,刷回了官方的rom,顿时更稳定了,以外的是这个版本的导航被切了,拍手叫好啊.输入法引入了搜狗的云输入,更方便了,官方的输 ...
- Jenkins Post Build网址
Hudson Post build taskhttps://plugins.jenkins.io/postbuild-taskThis plugin allows the user to execut ...
- Fluent UDF【8】:编译型UDF
UDF除了可以以解释的方式外,其还可以以编译的方式被Fluent加载.解释型UDF只能使用部分C语言功能,而编译型UDF则可以全面使用C语言的所有功能. 1 编译型UDF介绍 编译型UDF的构建方式与 ...
- c#中的 virtual override 和abstract 以及sealed
1.如果父类方法没有加virtual关键字,即不是一个虚方法,则在子类中只能隐藏基类方法,而不能覆盖. 2.如果父类方法加了virtual关键字,即它是一个虚方法,在子类中一样可以隐藏. 3.如果子类 ...
- Goldengate:ERROR 180 encountered commit SCN that is not greater than the highest SCN already processed
How to recover from Extract ERROR 180 encountered commit SCN that is not greater than the highest SC ...
- 【iCore1S 双核心板_ARM】例程八:ADC实验——电源监控
实验原理: STM32内部集成三个12位ADC,iCore1S的所有电源经过 电阻分压或者直接接入STM32的ADC的输出通道内,输入电流 经过高端电流检测芯片ZXCT1009F输入到ADC的输入通道 ...
- 30. Substring with Concatenation of All Words
题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...
- jquery checkbox checked 却不显示对勾
$("input").attr("checked", true); 或 $("input").attr("checked" ...