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>