How to Link Foreground to Property

I cannot bind a foreground color to a property ...

I have it:

<TextBox Grid.Column="1" Grid.Row="1" Width="150" Margin="10,5" 
         IsReadOnly="True" Name="Output" FontSize="20" Foreground="{Binding Path=ForegroundColor}"/>

and

private Brush foregroundColor;

public Brush ForegroundColor {
    get { return foregroundColor; }
    set {
        foregroundColor = value;
        OnPropertyChanged("ForegroundColor");
    }
}

private void CheckBtn_Click(object sender, RoutedEventArgs e) {
    if (IsPalindrome(Input.Text)) {
        ForegroundColor = Brushes.Gold;
        Output.Text = "Yep";                
    } else 
        Output.Text = "Nope";
}

I'm just a beginner, so this is a simple project;) Some sources are welcome to study ... sorry for the mistakes, I'm from Ukraine

+4
source share
1 answer

If you defined this property in your code behind the class, you can set DataContextyour window as follows:

<Window x:Class="YourWindow"
  Title="Your Title"
  DataContext="{Binding RelativeSource={RelativeSource Self}}">

After that, you should be able to bind this property that you are trying to do.

+2
source

Source: https://habr.com/ru/post/1626684/


All Articles