Question

I am trying to change the foreground color of a text block based on a bool property that its value is changed when a button is clicked. However, for some reason this is not working. Also, do I have to add the bool property to a list in first place? I tried adding the bool property directly to the DataContext, but this did not work either. Any help would be appreciated.

    public static bool IsOn { get; set; }
    public static List<bool> boo;
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsOn = true;
        boo = new List<bool>();
        boo.Add(IsOn); 
        DataContext = boo;                   
    }
<Window.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Green" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsOn}" Value="true">
                <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel>
        <Button Click="Button_Click" Content="Change Color" />
        <TextBlock Name="textBlockColor" Text="My Foreground Color" />
    </StackPanel>

Update: Following kmatyaszek's answer which worked fine, I found out that the following code works too, short and to the point:

    public bool IsOn { get; set;}
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsOn = true;
        textBlockColor.DataContext = this;
    }

Update2: As Viv suggested in the comments, I tried his approach by creating a separate class, and the following code worked just fine:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();  
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Test test = new Test();
        test.IsOn = true;
        textBlockColor.DataContext = test;
    }    
}

public class Test 
{
    public bool IsOn { get; set; }
}

Update3 The following approach will work as well as Viv suggested. In this case, properties are added to a list, I also added a second property for testing. However, this approach will require knowledge of the property associated with the used Index:

public partial class MainWindow : Window
{
    public List<bool> boo;
    public bool IsOn { get; set; }
    public bool IsBlue { get; set; }
    public MainWindow()
    {
        InitializeComponent();  
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsOn = true;
        IsGreen = true;
        boo = new List<bool>();
        boo.Add(IsOn);
        boo.Add(IsGreen);
        DataContext = boo;
    }
}

XAML

 <Window.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Green" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding [1]}" Value="true">
                <Setter Property="Foreground" Value="Blue" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
<StackPanel>
    <Button Click="Button_Click" Content="Change Color" />
    <TextBlock Name="textBlockColor" Text="My Foreground Color" />
</StackPanel>

Update4 The following code works as well and using a Dictionary instead of a List allows each item to be identified with a name of your choice:

public partial class MainWindow : Window
{
    public Dictionary<string,bool> booDictionary;
    public bool IsBlue { get; set; }
    public MainWindow()
    {
        InitializeComponent();  
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsBlue = true;
        booDictionary = new Dictionary<string,bool>();
        booDictionary.Add("Blue",IsBlue);
        DataContext = booDictionary;
    }
}

XAML

<Window.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Green" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding [Blue]}" Value="true">
                <Setter Property="Foreground" Value="Blue" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel>
        <Button Click="Button_Click" Content="Ok" />
        <TextBlock Name="textBlockColor" Text="Hey" />
    </StackPanel>
Was it helpful?

Solution

First of all your class must implement INotifyPropertyChanged (msdn) to notify view that your property was changed.

Second you must assign DataContext in MainWindow constructor.

Example:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;
    }

    private bool _isOn;
    public bool IsOn
    {
        get { return _isOn; }
        set { _isOn = value; OnPropertyChanged("IsOn"); }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IsOn = !IsOn;          
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propName));
    }
}

Your XAML code is OK.

When your binding doesn't work you should use snoop in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top