我需要一个可以改变TextBox前景的故事板。问题是这个TextBox必须在DataTemplate中。
如何更改我的xaml以使其正常工作?
<DataTemplate x:Key="contentTexBox">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Duration="0" To="Pink" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="tbContent"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBox Text="Test text" Width="200" Height="35" Foreground="Blue" x:Name="tbContent" BorderBrush="Purple">
</TextBox>
</Grid>
</DataTemplate>
答案 0 :(得分:-1)
我是用另一种方式做的,但是这个方法没有明确地引用文本框,所以我认为它应该适用于你的情况。主要更改是对样式化TextBox
的调用现在是隐式的(因为状态是在此控件下声明的)并且Background
属性已更改而不是Foreground
。
在调整了我写给你的案例的测试代码后,我想它看起来或多或少是这样的:
<DataTemplate x:Key="contentCheckBox">
<Grid>
<TextBox Text="Test text" Width="200" Height="35" Foreground="Blue" BorderBrush="Purple">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<TextBox Width="190">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Duration="0" To="Pink" Storyboard.TargetProperty="Background.Color" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</TextBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</DataTemplate>