如何使用触发器改变SolidcolorBrush资源的颜色?

5

我有一个用户控件,使用类似以下的画刷资源为控件中的多个元素提供颜色:

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

现在,我想通过触发器改变资源的颜色,在特定条件下提供高亮显示。这是否可行?如果是,怎么做呢?谢谢!

2
我认为你不能更改一个资源,创建另一个资源并在触发器条件下切换它们是否可行? - eran otzap
3个回答

4

您可以创建一支画笔,并将其"颜色"属性绑定到隐藏控件的"前景色.颜色"属性。当触发器执行时,该画笔可在任何地方使用并更改其颜色:

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

你真是个天才!非常感谢,那个技巧非常有效! - Genius

3

我不认为您可以从xaml中的触发器更改资源颜色。

您可以在代码后台更改颜色,或者将颜色设置为对象的数据绑定属性中的SolidColorBrush。

SolidColorBrush myBrush = (SolidColorBrush)this.TryFindResource("BlackBrush");

if (myBrush != null)
{
    myBrush.Color = Colors.Yellow;
}

否则,您需要根据触发器交换刷子。以下是一个例子:
   <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>

根据文本值更改背景颜色;如果文本是白色,那么背景就是白色,黑色则背景是黑色。


2

不,你不能在XAML中这样做,但问题是你想要根据其他控件的状态来改变某些控件。

你至少有以下几个选项(请查看此帖子):

  • 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>
    


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接