I have a user control that uses a brush resource like the following to provide the color for several elements in the control:
<UserControl.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="Black"/>
</UserControl.Resources>
Now, I'd like to change the color of this resource with a trigger to provide a highlight when a certain condition occurs.
Is this possible? If so, how?
Thanks!
No, you cannot do it XAML, but the problem is you want to change some controls based on state of the other control/controls.
You have at least the following options (take a look on this thread):
Also you can use EventTriggers on elements, and it looks like this:
<StackPanel>
<Label Margin="10" x:Name="lbl">My Label</Label>
<Button Width="150" Height="100" Background="Yellow" x:Name="btn1">My Button
</Button>
<StackPanel.Triggers>
<EventTrigger RoutedEvent="Button.MouseMove" SourceName="btn1">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="lbl" Storyboard.TargetProperty="(Label.Background)">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Yellow"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Button.MouseLeave" SourceName="btn1">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="lbl" Storyboard.TargetProperty="(Label.Background)">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="White"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</StackPanel.Triggers>
You can create a brush and bind its "Color" property to the "Foreground.Color" property of a hidden control. This brush can then be used anywhere and changes its color when the trigger is executed:
<Grid Background="Black">
<Grid.Resources>
<SolidColorBrush x:Key="BrushForeground" Color="{Binding ElementName=collapsedControl, Path=Foreground.Color}" />
<SolidColorBrush x:Key="BrushGreen" Color="Lime" />
<SolidColorBrush x:Key="BrushRed" Color="Red" />
</Grid.Resources>
<Control Name="collapsedControl" Visibility="Collapsed">
<Control.Style>
<Style TargetType="Control">
<Setter Property="Foreground" Value="{StaticResource BrushGreen}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsIncorrect}" Value="True">
<Setter Property="Foreground" Value="{StaticResource BrushRed}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Control.Style>
</Control>
<Label Content="Sample Text" Foreground="{StaticResource BrushForeground}" />
<Button Width="150"
Height="50"
Click="Button_Click"
Content="Set IsIncorrect to true" />
</Grid>
I don't think you can change the resource color from a trigger in xaml.
You can change the color in codebehind, or you set the color in your SolidColorBrush to a databound property of your object.
SolidColorBrush myBrush = (SolidColorBrush)this.TryFindResource("BlackBrush");
if (myBrush != null)
{
myBrush.Color = Colors.Yellow;
}
Otherwise, you need to swap the brushes based on a trigger. Below is an example:
<Grid Margin="50">
<Grid.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="Black"/>
<SolidColorBrush x:Key="WhiteBrush" Color="White"/>
<Style x:Key="test" TargetType="TextBlock">
<Setter Property="Background" Value="{StaticResource BlackBrush}"/>
<Style.Triggers>
<Trigger Property="Text" Value="white">
<Setter Property="Background" Value="{StaticResource WhiteBrush}"/>
</Trigger>
<Trigger Property="Text" Value="black">
<Setter Property="Background" Value="{StaticResource BlackBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<TextBlock
Height="20"
Margin="50"
Padding="50"
Style="{StaticResource test}"
Text="white">
</TextBlock>
</Grid>
This will change the background color based on the text value; if text is white, then the background is white, black, then background is black.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With