wpf 使用触发器更改 SolidcolorBrush 资源的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12754358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the color of a SolidcolorBrush resource with a trigger?
提问by HolySamosa
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!
谢谢!
回答by Michael G
I don't think you can change the resource color from a trigger in xaml.
我认为您无法通过 xaml 中的触发器更改资源颜色。
You can change the color in codebehind, or you set the color in your SolidColorBrush to a databound property of your object.
您可以在代码隐藏中更改颜色,或者将 SolidColorBrush 中的颜色设置为对象的数据绑定属性。
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.
这将根据文本值更改背景颜色;如果文本为白色,则背景为白色,黑色,则背景为黑色。
回答by Ivan Leonenko
No, you cannot do it XAML, but the problem is you want to change some controls based on state of the other control/controls.
不,您不能使用 XAML,但问题是您想根据其他控件/控件的状态更改某些控件。
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>
- 如果元素可以在一个内容模板中,您可以使用触发器并为 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>

