WPF中的DataGrid中Column的IsReadOnly和VIsibility等属性绑定时,总没有任何效果,输出里也显示找不到,搜了半天才发现,原来Column不属于visual 或 logical tree,所以不能在树上搜索他的绑定源,需要借助其他方法给属性指定数据源,方法如下:
<UserControl.Resources>
<local:BindingProxy x:Key="proxy" Data="{Binding}" />
</UserControl.Resources>
<DataGridTemplateColumn Visibility="{Binding Data.IsVisible,
Source={StaticResource proxy},
Converter={StaticResource BooleanToVisibilityConverter}}">
public class BindingProxy : Freezable
{
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
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));
}
原文地址: https://stackoverflow.com/questions/15494226/cannot-find-source-for-binding-with-reference-relativesource-findancestor