DataGridTextColumn - How to bind IsReadonly?

北城以北 提交于 2019-12-05 08:26:52

what you can do is to create an attached property to handle the change of the IsReadOnly property in the DataGridTextColumn.

public class Class1
{
    public static void SetIsReadOnly(DependencyObject obj, bool isReadOnly)
    {
        obj.SetValue(IsReadOnlyProperty, isReadOnly);
    }

    public static bool GetIsReadOnly(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsReadOnlyProperty);
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsReadOnlyProperty =
        DependencyProperty.RegisterAttached("IsReadOnly", typeof(bool), typeof(Class1), new PropertyMetadata(false, Callback));

    private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((DataGridTextColumn)d).IsReadOnly = (bool)e.NewValue;
    }
}

In your xaml, you can just use the attached property instead.

<sdk:DataGridTextColumn local:Class1.IsReadOnly="True" Binding="{Binding Property1}" Header="Property1"/>

Hope this helps. :)

Update

Once you have the DataContextProxy class, you do

        <sdk:DataGridTextColumn Binding="{Binding Name}"
                                local:Class1.IsReadOnly="{Binding DataSource.IsInEditMode, Source={StaticResource DataContextProxy}, Converter={StaticResource xxxConverter}}"
                                Header="ReadOnly Header" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!