In Xaml, how do you change the value of the SolidColorBrush programmatically?

199 Views Asked by At

Hi I have a xaml app that has one particular color used in many places on a page. I want to programmatically change the value of this color. I don't want to have to update every object's color individually. I have tried this:

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

Then in codebehind:

public Brush GalleryViewBrush { get; set; }

GalleryViewBrush = new SolidColorBrush(Colors.Red);

But the color never works. I have tried xbind too but no luck

thanks

1

There are 1 best solutions below

0
On

Your view model needs to implement INotifyPropertyChanged, and then fire the PropertyChanged event when your brush property changes. Conventionally that's done in the setter for the property.

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

If your get/set for GalleryViewBrush in your question is what's actually in the code sample in your question, that's likely to be the issue.

Mind typos in my code sample above, I'm hammered ATM.