【发布时间】:2016-04-05 22:16:16
【问题描述】:
我有这个按钮,在背景画笔上有一个双向绑定,我已经设置了一个依赖属性,我也在使用 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
感谢任何帮助。
【问题讨论】:
标签: c# wpf data-binding