我需要改变鼠标的颜色/点点的内容演示者,但我的风格不起作用。
谁来帮我?
谢谢
<Style x:Key="Test" TargetType="ContentPresenter">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ColorTest}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ColorTest}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Style>发布于 2015-10-25 20:18:56
我认为这是不可能的。可视化状态由特定控件发布,ContentPresenter表示任何类型的控件或任意复杂的元素树。
您可以使用控件样式和模板中的子节来了解哪个Visual对每个控件有效,但正如您所看到的,这些都是特定于所讨论的控件的,而且并不总是支持每个状态。
您的样式可以修改为使用触发器(如<Trigger Property="IsMouseOver" Value="True"> ),但是您只能为ContentPresenter的属性提供设置器,而Foreground不是其中之一。
更新
但是,由于TextBlock.Foreground是附加属性,因此可以使触发器解决方案在特定示例中工作,并包含复杂的控件内容。但请注意,这并不适用于所有属性。
<Grid>
<Grid.Resources>
<ControlTemplate TargetType="Button" x:Key="ButtonControlTemplate">
<ContentPresenter />
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextBlock.Foreground" Value="Red" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="TextBlock.Foreground" Value="Blue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Grid.Resources>
<Button Template="{StaticResource ButtonControlTemplate}" Content="Test" />
</Grid>https://stackoverflow.com/questions/31744145
复制相似问题