0

I want to bind the Fill property in a style to a public property, I've used the following way but it seems like it doesn't work.

public SevenSegmentControl()
{
    InitializeComponent();
    SevenSegmentColor = Color.FromRgb(251, 23, 23);
}

public Color SevenSegmentColor { get; set; }
<Style x:Key="RectangleStyle1" TargetType="{x:Type Rectangle}">
    <Setter Property="Fill" Value="{Binding Path=SevenSegmentColor, Mode=TwoWay}"/>
    <Setter Property="RadiusX" Value="4"/>
    <Setter Property="RadiusY" Value="4"/>
    <Setter Property="StrokeThickness" Value="0"/>
    <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
</Style>

How can I do it?

H.B.
  • 136,471
  • 27
  • 285
  • 357
Mohammad Dayyan
  • 18,338
  • 35
  • 143
  • 207

1 Answers1

2

You are trying to set a color value directly to the Fill property,which is of type Brush.You can inspect the Output window to find out the binding errors.Either you need a valueconverter to convert your color to a valid brush or you need to do like this

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SevenSegmentColor =new SolidColorBrush(Color.FromRgb(251, 23, 23));
            DataContext=this;
        }

        public SolidColorBrush SevenSegmentColor { get; set; }

    }

EDIT

If you are assigning the Datacontext before the Property value is set,the UI will never be notified in your Property change.As in your case you are assigning the datacontext using

DataContext="{Binding RelativeSource={RelativeSource Self}}"

So the Datacontext is set at the initialization time itself before your SevenSegmentColor property is assigned a value.Later after initialization when you assigned a color value to your Property, the UI will never get notified and hence your color is not shown in the UI.To solve this you need to implement the INotifyPropertyChanged Interface in your UserControl

Sample

 /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        private SolidColorBrush sevenSegmentColor;
        public MainWindow()
        { 
            InitializeComponent();
           SevenSegmentColor =new SolidColorBrush(Color.FromRgb(251, 23, 23));
        }

        public SolidColorBrush SevenSegmentColor
        {
            get
            {
                return sevenSegmentColor;
            }
            set
            {
                sevenSegmentColor = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged("SevenSegmentColor");
            }
        }

        // Declare the event
        public event PropertyChangedEventHandler PropertyChanged;

        // Create the OnPropertyChanged method to raise the event
        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
Community
  • 1
  • 1
biju
  • 16,259
  • 9
  • 56
  • 92