Home > Mobile >  Why is the color of my button which is bind to an element of a brush array is not showing in the UI?
Why is the color of my button which is bind to an element of a brush array is not showing in the UI?

Time:02-03

I am trying to change button's color when it is clicked. But It's not changing color. Here is my code.

private SolidColorBrush[] _btnBrush;
        public SolidColorBrush[] btnBrush
        {
            get
            {
                return _btnBrush;
            }
            set
            {
                _btnBrush = value; OnPropertyChanged("btnBrush");
            }
        }

     public MineViewModel()
        {
         for (int i = 0; i < 100; i  )
             btnBrush[i] = (SolidColorBrush)new BrushConverter().ConvertFrom("#EED6C4");
}

the method which button is bind to

public void LeftClick(string id_no)
        {
          btnBrush[Convert.ToInt32(id_no)] = (SolidColorBrush)new BrushConverter().ConvertFrom("#B3541E");
        }

and for XAML

<Button x:Name="btn4" Background="{Binding btnBrush[3]}" Command="{Binding LeftClickCommand}" CommandParameter="3"/>

This is executing correctly, the element in array also got the color. But it won't show on the UI. If I bind it with a single solid brush, it works. But not with array of brushes. Any help?

CodePudding user response:

The expression

btnBrush[i] = (SolidColorBrush)new BrushConverter().ConvertFrom("#B3541E");

does not change the value of the btnBrush property. It does not even call the property setter.

You may however change the Color property of the SolidColorBrush at element index i, like

btnBrush[i].Color = (Color)new ColorConverter().ConvertFrom("#B3541E");

This will notify the UI because Color is a dependency property. Otherwise the array element type would have to implement the INotifyPropertyChanged interface and fire a change notification.

  •  Tags:  
  • Related