嗨,我有一个xaml应用程序,有一种特殊的颜色在页面上的许多地方使用。我想以编程方式更改此颜色的值。我不想单独更新每个对象的颜色。我已经尝试过了:
<Grid
Background="{Binding GalleryViewBrush, Mode=TwoWay}"
Grid.Row="0"
Grid.Column="0">然后在代码背后:
公共画笔GalleryViewBrush { get;set;}
GalleryViewBrush =新的SolidColorBrush(Colors.Red);
但是这个颜色永远不会起作用。我也尝试过xbind,但没有成功
谢谢
发布于 2016-04-16 11:06:26
视图模型需要实现INotifyPropertyChanged,然后在画笔属性更改时触发PropertyChanged事件。通常,这是在属性的setter中完成的。
private Brush _myBrush;
public MyBrush {
get { return _myBrush; }
set {
_myBrush = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyBrush));
}
}如果问题中GalleryViewBrush的get/set是问题中代码示例中的实际内容,那么这可能就是问题所在。
注意上面代码示例中的输入错误,我被自动取款机撞坏了。
https://stackoverflow.com/questions/36659125
复制相似问题