在咱们wpf开发中,不少人会有mvvm模式去作wpf的项目。mvvm
是否有人遇到这样一个场景:在一个界面上,有个tabcontrol上面有4个页签,每一个页签里面都有一个datagrid,里面显示的列基本同样,绑定的数据集合都是同一个,可是有个差别,在第二个页签上须要第二列不显示,第三个页签只显示一个列。ide
咱们若是用的是mvvm,这个时候就会去使用数据绑定,问题在于咱们怎么让datagrid的下一级也就是DataGridTextColumn识别到vm,或者怎么在模板列中识别vm,能够绑定咱们在vm中声明的显示隐藏属性。spa
这里有个方案:代理
public class BindingProxy : Freezable { protected override Freezable CreateInstanceCore() { throw new NotImplementedException(); } public object Data { get { return (object)GetValue(DataProperty); } set { SetValue(DataProperty, value); } } // Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc... public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null)); }
很简单的方式,定义一个空对象用来转接vm,将vm存入Data。code
下面是用法:对象
很简单的方式。可是这个的使用场景仍是比较多的,在不少绑定操做的时候,由于层级的关系有时候不必定能找到须要的对象,经过这个代理作一个转接的做用。blog
有对WPF感兴趣的同窗,能够加页面下方的qq群,咱们一块儿共同进步!开发