[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 ...
随机推荐
- [Aaronyang] 写给自己的WPF4.5 笔记12[自定义控件-AyImageButton的过程 2/4]
我的文章一定要做到对读者负责,否则就是失败的文章 --------- www.ayjs.net aaronyang技术分享 博文摘要:点击前往文章正文 学会怎样给用户提供事件接口,本例子 ...
- Effective Java 第三版——52. 明智而审慎地使用重载
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- 安装配置OSA运维管理平台
1.下载完整包V1.0.2wget http://www.osapub.com/download/OSA_BETA_V1.0.2.tar.gzV1.0.5wget http://www.osapub. ...
- Linux安装R记要
R在Linux上的安装有一些坑(Windows上安装会方便许多),在这里记录,希望可以减少读者不必要的麻烦.我的服务器是SUSE Linux 64位,无法接入互联网(安全原因,你懂的). 到R官网ht ...
- Maven使用详解,非常详细
本文转:http://blog.csdn.net/u010425776/article/details/52027706 什么是Maven? 如今我们构建一个项目需要用到很多第三方的类库,如写一个使用 ...
- pycharm启动慢 –xms -xmx相关参数设置
Eclipse崩溃,错误提示:MyEclipse has detected that less than 5% of the 64MB of Perm Gen (Non-heap memory) sp ...
- Linux初始化init系统-Sysvinit、Upstart、Systemd
首先了解以下Ubuntu运行级别(init)对应工具的变化历史: 1.Ubuntu 6.10及以前版本使用Sysvinit. 2.Ubuntu 14.10及以前版本使用Upstart但是还留着Sysv ...
- mysql解决大量time_wait
mysql解决大量time_wait 命令查看TIME_WAIT连接数 netstat -ae|grep "TIME_WAIT" |wc -l 早上登陆服务器的时候输入ne ...
- 十分钟学会Charles抓包(iOS的http/https请求)
### 原文地址,感谢作者 : http://www.jianshu.com/p/5539599c7a25 Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装 官网下载安装Ch ...
- JConsole & JVisualVM远程监视Websphere服务器JVM的配置方法
原文链接:http://xjsunjie.blog.51cto.com/999372/1331880/ jconsole是JDK里自带的一个工具,可以监测Java程序运行时所有对象的申请.释放等动作, ...