我有一个简单的buttonStyle 定义TargetType
of Button
;但将样式设置为按钮会产生例外。
<Window>
<Window.Resources>
<Style x:Key="buttonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Magenta"/>
</Style>
</Window.Resources>
<StackPanel Orientation="Horizontal">
<Button Content="1" FocusVisualStyle="{StaticResource buttonStyle}"/>
</StackPanel>
</Window>
附加信息:“按钮”TargetType 与类型不匹配
元素“控制”。
此外,设置TargetType
as Control
消除了运行时错误,但按钮的视觉样式在获取时不会改变Focus
.
样式设置为时有效Button.Style
Edit我有两个具体问题:
我同意这样一个事实FocusVisualStyle
是一个属性FrameworkElement
and FrameworkContentElement
,但是为什么在按钮上设置它时会出现错误,尽管 Style 是一个命名样式而不是一个打字风格 ?
为什么FocusVisualStyle
不要渲染在Button
?是个Button.FocusVisualStyle
是否被任何更高优先级值(如模板、触发器或模板触发器)内部覆盖?
a FocusVisualStyle
允许您在出现问题时向用户提供视觉反馈control是专注的。例如,添加一个Rectangle
它看起来像控件的边框。
A Style
是控件本身的外观和感觉。一切都已经解释了here https://msdn.microsoft.com/en-us/library/bb613567(v=vs.110).aspx.
FocusVisualStyle
不是的风格Button
就其本身而言,这是当Button
是专注的。
See here https://msdn.microsoft.com/en-us/library/ms744790(v=vs.110).aspx了解更多信息。
我认为你所追求的是Trigger
.
<Style x:Key="buttonStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Magenta"/>
</Trigger>
</Style.Triggers>
</Style>
然后,您可以设置Style
你的Button
,像这样:
<Button Style="{StaticResource buttonStyle}" ... />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)