大卫。我已经创建了一个示例窗口,它使用
TextBox.Style
:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Window.Resources>
<SolidColorBrush x:Key="IsCheckedColor" Color="DarkGray" />
</Window.Resources>
<StackPanel>
<TextBox x:Name="textbox" Margin="36" Height="24" >
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Background" Value="White" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=checkbox}" Value="True" >
<Setter Property="Background" Value="{StaticResource IsCheckedColor}" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<CheckBox x:Name="checkbox" Content="Click Me" Height="24" Margin="36"/>
</StackPanel>
</Window>
Trigger
更改另一个控件的属性,但可以使用它们基于其他内容更改控件的属性,例如
DataContext
或其他控件。
每个控件可以有一个
Triggers
集合,但只能包含
EventTriggers
. 在一个
Style
你可以用普通的
它可以用来控制动画,以及
DataTrigger
,我在这个示例中使用它来控制
TextBox
基于属性的设置
CheckBox
.
Setter
外面的
集合来设置默认值,我不需要任何时间
塞特
当
没有被选中——它只是返回到“默认”状态。
我在Blend中做这个,但是如果你没有Blend,你当然可以直接把XAML放进去。这与控制状态有关。作为
文本框
Normal
MouseOver
,和
Disabled
,可以设置外观更改的动画。在本例中,我们使用了一个几乎为零持续时间的动画,因此更改是即时的。
将以下内容添加到
StackPanel
:
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup">
<VisualState x:Name="Disabled">
<Storyboard>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="textbox" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="{StaticResource IsCheckedColor}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>