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));
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…