我有一个用户控件,使用类似以下的画刷资源为控件中的多个元素提供颜色:
<UserControl.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="Black"/>
</UserControl.Resources>
现在,我想通过触发器改变资源的颜色,在特定条件下提供高亮显示。这是否可行?如果是,怎么做呢?谢谢!
我有一个用户控件,使用类似以下的画刷资源为控件中的多个元素提供颜色:
<UserControl.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="Black"/>
</UserControl.Resources>
您可以创建一支画笔,并将其"颜色"属性绑定到隐藏控件的"前景色.颜色"属性。当触发器执行时,该画笔可在任何地方使用并更改其颜色:
<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>
我不认为您可以从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>
根据文本值更改背景颜色;如果文本是白色,那么背景就是白色,黑色则背景是黑色。
不,你不能在XAML中这样做,但问题是你想要根据其他控件的状态来改变某些控件。
你至少有以下几个选项(请查看此帖子):
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>