【问题标题】:Change the color of a SolidcolorBrush resource with a trigger?使用触发器更改 SolidcolorBrush 资源的颜色?
【发布时间】:2012-10-05 21:35:10
【问题描述】:

我有一个用户控件,它使用如下画笔资源为控件中的多个元素提供颜色:

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

现在,我想使用触发器更改此资源的颜色,以便在特定条件发生时提供高亮显示。

这可能吗?如果有,怎么做?

谢谢!

【问题讨论】:

  • 我认为您不能更改资源,是否可以创建另一个资源并根据触发器的条件在它们之间进行交换?

标签: .net wpf silverlight xaml


【解决方案1】:

我认为您不能通过 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,但问题是您想根据其他控件/控件的状态更改某些控件。

    您至少有以下选项 (take a look on this thread):

    • 如果元素可以在一个内容模板中,您可以使用触发器并为 setter 提供 SourceName 和 TargetName 以指向目标元素
    • 你也可以在元素上使用 EventTriggers,它看起来像这样:

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

    【讨论】:

      【解决方案3】:

      您可以创建画笔并将其“Color”属性绑定到隐藏控件的“Foreground.Color”属性。然后可以在任何地方使用此画笔,并在执行触发器时更改其颜色:

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

      【讨论】:

      • 你是天才!非常感谢,这个技巧就像一个魅力!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      相关资源
      最近更新 更多