【问题标题】:In Xaml, how do you change the value of the SolidColorBrush programmatically?在 Xaml 中,如何以编程方式更改 SolidColorBrush 的值?
【发布时间】:2016-08-08 03:12:25
【问题描述】:

您好,我有一个 xaml 应用程序,它在页面上的许多地方使用了一种特定颜色。我想以编程方式更改此颜色的值。我不想单独更新每个对象的颜色。我试过这个:

<Grid
   Background="{Binding GalleryViewBrush, Mode=TwoWay}"
   Grid.Row="0"
   Grid.Column="0">

然后在代码隐藏中:

公共画笔 GalleryViewBrush { get;放; }

GalleryViewBrush = new SolidColorBrush(Colors.Red);

但颜色永远不会起作用。我也试过 xbind 但没有运气

谢谢

【问题讨论】:

  • 并从绑定中删除Mode=TwoWay。这没有任何意义。

标签: xaml binding solidcolorbrush


【解决方案1】:

您的视图模型需要实现INotifyPropertyChanged,然后在您的画笔属性更改时触发PropertyChanged 事件。通常这是在属性的设置器中完成的。

private Brush _myBrush;
public MyBrush {
    get { return _myBrush; }
    set {
        _myBrush = value;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyBrush));
    }
}

如果您在问题中对GalleryViewBrush 的获取/设置是您问题的代码示例中的实际内容,则可能是问题所在。

请注意上面代码示例中的拼写错误,我被 ATM 锤击了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多