我有一个用户控件,它使用如下所示的画笔资源来为控件中的几个元素提供颜色:
<UserControl.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="Black"/>
</UserControl.Resources>
现在,我想用触发器更改此资源的颜色,以便在发生某种情况时提供突出显示。
这可能吗?如果是这样,怎么样?
谢谢!
答案 0 :(得分:2)
我认为你不能改变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>
这将根据文本值更改背景颜色;如果文字为白色,则背景为白色,黑色,背景为黑色。
答案 1 :(得分:2)
不,你不能做XAML,但问题是你想根据其他控件/控件的状态更改一些控件。
您至少有以下选项(take a look on this thread):
您还可以在元素上使用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>
答案 2 :(得分:0)
您可以创建画笔并将其“ 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>