繁体   English   中英

wpf控件背景2向绑定只能以一种方式工作

[英]wpf control background 2-way binding only working one way

我有一个在背景画笔上具有双向绑定的按钮,我已经设置了一个依赖项属性,我也正在使用INotifyPropertyChanged接口。 但是我仍然对双向绑定有疑问。

如果我更新绑定到按钮的属性,则按钮背景会发生变化,就像我期望的那样,但是如果我直接更新按钮背景(“ button.Background = Brushes.Blue”),则该属性不会更新。

这是按钮的xaml:

<Button Background="{Binding ElementName=MainWindow,Path=TitleBrush,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

物业:

public Brush TitleBrush 
{
    get 
    {
        return (Brush)GetValue(TitleBrushProperty);
    }
    set 
    {
        if (!_graph.TitleBrush.Equals(value)) 
        {
            _graph.TitleBrush = value;
            SetValue(TitleBrushProperty, value);
            NotifyPropertyChanged(nameof(TitleBrush));
        }
    }
}

public static readonly DependencyProperty TitleBrushProperty =
        DependencyProperty.Register(nameof(TitleBrush), typeof(Brush), typeof(MainWindow));

我更改背景颜色的两种方法:

TitleBrush = Brushes.Red; // This works great
button.Background = Brushes.Red; // This changes the background but doesn't update the property

任何帮助表示赞赏。

TwoWay用于将值设置为绑定属性并接受值。 在您的情况下,您将两种情况都设置为一种方式-设置为控件。 控件没有发送回属性值。 TwoWay确实适用于输入字段。 如果使用的是后台代码,则应使用bound属性。

BindingMode枚举文档所述:

导致对源属性或目标属性的更改以自动更新另一个。 这种类型的绑定适用于可编辑表单或其他完全交互的UI方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM