Change the color of a SolidcolorBrush resource using a trigger?

I have a user control that uses a brush resource, such as the following, to provide color for multiple elements in the control:

<UserControl.Resources> <SolidColorBrush x:Key="BlackBrush" Color="Black"/> </UserControl.Resources> 

Now I would like to change the color of this resource using a trigger to highlight the selection when a certain condition occurs.

Is it possible? If so, how?

Thanks!

+4
source share
2 answers

No, you cannot do this XAML, but the problem is that you want to change some controls based on the state of another control / controls.

You have at least the following options (take a look at this thread ):

  • If elements can be in the same content template, you can use triggers and provide SourceName and TargetName for setters to point to the target element
  • You can also use EventTriggers for 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> 

+2
source

I don't think you can change the color of a resource from a trigger in xaml.

You can change the color in codebehind or set the color in SolidColorBrush for the database property of your object.

 SolidColorBrush myBrush = (SolidColorBrush)this.TryFindResource("BlackBrush"); if (myBrush != null) { myBrush.Color = Colors.Yellow; } 

Otherwise, you need to change the brushes based on the trigger. The following 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 the text is white, then the background is white, black, then the background is black.

+1
source

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


All Articles