简体   繁体   中英

WPF binding a background color initializes but not updating

I am using observable collection and dataTemplates to create generic panels, I have encountered an issue when my buttons colors (setted programaticly) do initializes, but once changed while running the color stays still.

Here is my Xaml code for the button:

<Button Grid.Column="0" Grid.Row="3" Background="{Binding Path=motionColor}" Command="{Binding Path=MotionPress}" Content="Motion" Height="51" Width="104" Style="{StaticResource MyButtonStyle}"/>

That's my DataTemplate:

<DataTemplate x:Key="GenericPanelTemplate"  DataType="local:GenericPanel">

And here's the relevant backend code in GenericPanel.cs:

motionColor:

public Brush motionColor { get; set; }

The function eventually the button execute (tested and running):

public void MotionButton_Click()
        {
                motionColor = setColor((byte)Value.ON);
        }
    private Brush setColor(byte value)
    {
        switch (value)
        {
            case (byte)Value.OFF:
                return Brushes.Red;
            case (byte)Value.ON:
                return Brushes.Green;
            case (byte)Value.PROGRESS:
                return Brushes.Yellow;
            default:
                return Brushes.Gray;
        }
    }

On start, the motionColor sets to be red, and the button does appear red, but when the value changed to Brushes.Green here it doesnt work anymore (debugging proves that the value changes correctly).

How do I make the button to check it's binded value and update the Background attribute?

If more code is needed please ask, I think these functions and Xaml lines are the only relevant ones.

Thanks for the help.

You must implement INotifyPropertyChanged ( MSDN link) on the class containing the property, and notify the changes to your property.

Something like:

public GenericPanel : INotifyPropertyChanged
{
  private Brush _motionColor;
  public Brush motionColor { 
    get { return _motionColor; }; 
    set { 
      _motionColor = value; 
      OnPropertyChanged(); 
    }
  }

  /* ... the rest of your class ... */

  public event PropertyChangedEventHandler PropertyChanged;
  protected void OnPropertyChanged([CallerMemberName] string propertyName = null)  
  {        
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  }
}

This all can be done automatically using Fody (specifically the PropertyChanged add-in ) but if you only require it for this specific property, using Fody might be overkill

Update

Since the OP, as per the comments, seems to be using older versions of .NET and C#, here's the "more compatible" version:

public GenericPanel : INotifyPropertyChanged
{
  private Brush _motionColor;
  public Brush motionColor { 
    get { return _motionColor; }; 
    set { 
      _motionColor = value; 
      OnPropertyChanged("motionColor"); 
    }
  }

  /* ... the rest of your class ... */

  public event PropertyChangedEventHandler PropertyChanged;
  protected void OnPropertyChanged(string propertyName)  
  {
    var handle = PropertyChanged;
    if(handle != null) 
      handle(this, new PropertyChangedEventArgs(propertyName));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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