欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF DataGrid中列的属性绑定问题

程序员文章站 2022-03-07 11:13:24
...

WPF中的DataGrid中Column的IsReadOnly和VIsibility等属性绑定时,总没有任何效果,输出里也显示找不到,搜了半天才发现,原来Column不属于visual 或 logical tree,所以不能在树上搜索他的绑定源,需要借助其他方法给属性指定数据源,方法如下:

原文地址:https://*.com/questions/15494226/cannot-find-source-for-binding-with-reference-relativesource-findancestor


<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://*.com/questions/15494226/cannot-find-source-for-binding-with-reference-relativesource-findancestor