场景:根据配置文件显示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的绑定的更多相关文章

  1. WPF DATAGrid 空白列 后台绑定列 处理

    原文:WPF DATAGrid 空白列 后台绑定列 处理 AutoGenerateColumns <DataGrid x:Name="dataGrid" Margin=&qu ...

  2. WPF DataGrid 双击行 获得绑定数据

    原文:WPF DataGrid 双击行 获得绑定数据 1)增加事件 2)增加对象获取 1)事件代码 Datagrid 增加事件 MouseDoubleClick="dataGrid_Mous ...

  3. 【WPF】WPF DataGrid List数据源 双向绑定通知机制之ObservableCollection使用以及MultiBinding 的应用

    以下代码实现了DataGrid的简单绑定List数据源 重点要提一下的是,绑定List数据源,但是不能直接用List.比如下面的代码,使用List<GridItem>只能实现数据修改的绑定 ...

  4. WPF DataGrid、ListView 简单绑定

    DataGrid运行效果: xaml 代码: DataGridName= dtgData ItemsSource= {Binding} AutoGenerateColumns= False DataG ...

  5. WPF Datagrid 动态生成列 并绑定数据

    原文:WPF Datagrid 动态生成列 并绑定数据 说的是这里 因为列头是动态加载的 (后台for循环 一会能看到代码) 数据来源于左侧列 左侧列数据源 当然num1 属于临时的dome使用  可 ...

  6. WPF DataGrid 绑定数据及时更新的处理

    原文:WPF DataGrid 绑定数据及时更新的处理 默认情况下datagrid 绑定数据源后,在界面编辑某一列后,数据不会及时更新到内存对象中.如在同一行上有一个命令对来获取 当前选中行(内存对象 ...

  7. WPF DataGrid绑定及列居中

    基本的数据绑定 把集合的字段(属性)绑定在DataGrid的Binding属性就能将数据绑定列表 public class CashItem { public int Value { get; set ...

  8. WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.

    WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次  悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...

  9. WPF DataGrid 绑定DataSet数据 自动生成行号

    1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...

随机推荐

  1. SNF快速开发平台MVC-EasyQuery-拖拽生成SQL脚本

    在之前介绍一下EasyQuery工具SNF开发平台WinForm-EasyQuery统计分析-效果-非常牛逼的报表查询工具 Winform开发框架之图表报表在线设计器-报表-SNF.EasyQuery ...

  2. [svc]二三层数据格式&&三层数据如何匹配路由

    网络知识拾遗 tcpip的4&7层模型 PDU数据包在不同层的不同称呼 物理层(一层)PDU指数据位(Bit). 数据链路层(二层)PDU指数据帧(Frame). 网络层(三层)PDU指数据包 ...

  3. windows后门

    原文:揭秘Windows系统的四个后门 组策略欺骗后门 创建一个批处理文件add.bat,内容是: @echo off net user hack$ test168 /add net localgro ...

  4. 【iCore1S 双核心板_ARM】例程五:IWDG看门狗实验——复位ARM

    实验原理: STM32内部包含独立看门狗,通过看门狗可以监控程序运行,程序错误 时,未在规定时间喂狗,自动复位ARM.本实验通过按键按下,停止喂狗, 制造程序运行 错误,从而产生复位 . 实验现象: ...

  5. Java知多少(96)绘图之设置字型和颜色

    Java绘图中,显示文字的方法主要有三种:(1)drawString(String str,int x,int y):在指定的位置显示字符串.(2)drawChars(char data[],int ...

  6. “failed to excute script xxx” PyInstaller 打包python程序为exe文件过程错误

    在使用PyInstaller打包python程序,打包命令为: pyinstaller -F -w -i manage.ico yourpyfile.py 顺便说一下几个参数的作用 -F:是直接生成单 ...

  7. db2 索引

    索引:可通过 SYSCAT.INDEXES JOIN SYSCAT.INDEXCOLUSE来查询索引的字段有升序ASC和降序DESC,分别表示为SYSCAT.INDEXES的COLNAMES中索引字段 ...

  8. mysql按月查询

    SELECT DATE_FORMAT(GenerateTime, '%m') as month, SUM(GenerateCount) AS count FROM identitycodetask ' ...

  9. 13组合模式Composite

    一.什么是组合模式 Composite模式也叫组合模式,是构造型的设 计模式之一.通过递归手段来构造树形的对象结 构,并可以通过一个对象来访问整个对象树. 二.组合模式的结构 三.组合模式的角色和职责 ...

  10. 8 -- 深入使用Spring -- 5...3 使用@CacheEvict清除缓存

    8.5.3 使用@CacheEvict清除缓存 被@CacheEvict注解修饰的方法可用于清除缓存,使用@CacheEvict注解时可指定如下属性: ⊙ value : 必须属性.用于指定该方法用于 ...