TargetType 与元素类型不匹配 [英] TargetType does not match type of element
本文介绍了TargetType 与元素类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我为 Button
的 TargetType
定义了一个简单的 buttonStyle;但是将样式设置为 button 是一个例外.
<窗口.资源><Style x:Key="buttonStyle" TargetType="{x:Type Button}"><Setter Property="Background" Value="Magenta"/></风格></Window.Resources><StackPanel Orientation="水平"><Button Content="1" FocusVisualStyle="{StaticResource buttonStyle}"/></StackPanel></窗口>
<块引用>
附加信息:'Button' TargetType 与元素控制".
<小时>
此外,将TargetType
设置为Control
可消除运行时错误,但按钮的视觉样式在获得Focus
.
样式在设置为 Button.Style
编辑我有两个具体问题:
我同意
FocusVisualStyle
是FrameworkElement
和FrameworkContentElement
的属性这一事实,但是为什么设置它会出错在按钮上,尽管 Style 是 namedstyle 而不是 typedstyle ?为什么
FocusVisualStyle
没有在Button
上呈现?Button.FocusVisualStyle
是否被任何更高优先级的值(如模板、触发器或模板触发器)在内部覆盖?
解决方案
a FocusVisualStyle
允许您在 控件 获得焦点时向用户提供视觉反馈.例如,添加一个看起来像控件边框的 Rectangle
.
Style
是控件本身的外观和感觉.在此处都有说明.
FocusVisualStyle
不是 Button
本身的样式,而是 Button
聚焦时的样式.
请参阅此处了解更多信息.
我认为你所追求的是一个触发器
.