Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the color of a SolidcolorBrush resource with a trigger?

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!

like image 541
HolySamosa Avatar asked Oct 05 '12 21:10

HolySamosa


3 Answers

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):

  • If elements can be in one content template, you can use Triggers and provide SourceName and TargetName for setters to point to target element
  • 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>
    

like image 43
Ivan Leonenko Avatar answered Oct 19 '22 01:10

Ivan Leonenko


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>
like image 28
HHenn Avatar answered Oct 19 '22 01:10

HHenn


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.

like image 112
Michael G Avatar answered Oct 19 '22 03:10

Michael G