我想根据不同的状态(正常,MouseOver,禁用等)更改文本框的边框颜色。我的代码看起来像这样
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"></VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Target="txttester.BorderBrush" Value="Red"></Setter>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="MouseOver">
<VisualState.Setters>
<Setter Target="txttester.BorderBrush" Value="Green"></Setter>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled"/>
<VisualState x:Name="ReadOnly"/>
</VisualStateGroup>
<VisualStateGroup x:Name="ValidationStates">
<VisualState x:Name="Valid"/>
<VisualState x:Name="InvalidFocused"/>
<VisualState x:Name="InvalidUnfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBox Height="40" Width="300" x:Name="txttester" ></TextBox>
</Grid>
对我来说一切看起来不错,但是边框颜色没有改变,我在这里做错了什么?
如果使用VisualState.Setters
控制UIElement的外观,并想应用特定的VisualState,则需要调用GoToState在状态之间进行转换。例如:
VisualStateManager.GoToState(this, "PointerOver", true);