wpf TargetType 与元素类型不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30804752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:12:30  来源:igfitidea点击:

TargetType does not match type of element

wpfxaml

提问by Marshal

I have a simply buttonStyle defined for TargetTypeof Button; but setting the style to button gives an exeption.

我为TargetTypeof定义了一个简单的 buttonStyle Button;但是将样式设置为 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>

Additional information: 'Button' TargetType does not match type of element 'Control'.

附加信息:“Button”TargetType 与元素“Control”的类型不匹配。



Further, setting the TargetTypeas Controlremoves the run-time error, but visual style of button doesn't change when it gets Focus.

此外,设置TargetTypeasControl消除了运行时错误,但按钮的视觉样式在它获取时不会改变Focus



Style works when set as Button.Style

样式设置为 Button.Style



EditI have two specific questions:

编辑我有两个具体问题:

  1. I agree to the fact that FocusVisualStyleis a property of FrameworkElementand FrameworkContentElement, but why is there an error setting it on button, despite the fact that Style is a namedstyleand not a typedstyle?

  2. Why does FocusVisualStyledon't get rendered on the Button? Is the Button.FocusVisualStyleover-ridden internally by any higher priority value like Templates, Triggers or Template Triggers ?

  1. 我同意这FocusVisualStyleFrameworkElementand 的一个属性的事实FrameworkContentElement,但是为什么在按钮上设置它会出错,尽管 Style 是namedstyle而不是typedstyle

  2. 为什么没有FocusVisualStyle在 上呈现Button?是否在Button.FocusVisualStyle内部被任何更高优先级的值(如模板、触发器或模板触发器)覆盖?

采纳答案by Mike Eason

a FocusVisualStyleallows you to provide visual feedback to the user when a controlis focused. For example, adding a Rectanglewhich looks like a border of the control.

aFocusVisualStyle允许您在控件聚焦时向用户提供视觉反馈。例如,添加一个Rectangle看起来像控件的边框。

A Styleis the look and feel of the control itself. It's all explained here.

AStyle是控件本身的外观和感觉。这一切都在这里解释。

FocusVisualStyleis not the style for the Buttonitself, it's the style for when the Buttonis focused.

FocusVisualStyle不是Button它本身的风格,Button而是聚焦时的风格。

See herefor more information.

请参阅此处了解更多信息。

I think what you are after is a Trigger.

我认为你所追求的是一个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>

Then, you can set the Styleof your Button, like so:

然后,您可以设置Style您的Button,如下所示:

<Button Style="{StaticResource buttonStyle}" ... />

回答by Olaru Mircea

You should use it like this

你应该像这样使用它

        <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="Magenta" />
                </Trigger>
            </Style.Triggers>
        </Style>

and set it like this:

并像这样设置:

<Button Content="1" Style="{StaticResource buttonStyle}"/>

To see this better:

为了更好地看到这一点:

         <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Magenta" />
                </Trigger>
            </Style.Triggers>
        </Style>

From MSDN

来自 MSDN

The focus visual style feature provides a common "object model" for introducing visual user feedback based on keyboard navigation to any UI element. This is possible without applying a new template to the control, or knowing the specific template composition.

焦点视觉样式功能提供了一个通用的“对象模型”,用于将基于键盘导航的视觉用户反馈引入任何 UI 元素。这是可能的,无需将新模板应用到控件,也无需了解特定的模板组成。

回答by Daap

To illustrate the difference between button and focus visual style, consider this:

为了说明按钮和焦点视觉样式之间的区别,请考虑以下内容:

<Style x:Key="rectFocusVisual" TargetType="{x:Type Rectangle}">
  <Setter Property="Margin" Value="2" />
  <Setter Property="SnapsToDevicePixels" Value="true" />
  <Setter Property="Stroke" Value="Red" />
  <Setter Property="StrokeThickness" Value="1" />
  <Setter Property="StrokeDashArray" Value="1 3" />
</Style>
<Style x:Key="focusVisual">
  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate>
        <Rectangle Style="{StaticResource rectFocusVisual}" />
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
<Style x:Key="button" TargetType="{x:Type Button}">
  <Setter Property="Background" Value="Green" />
</Style>

and:

和:

<Button
  Content="I am a green button with red focus rectangle"
  FocusVisualStyle="{StaticResource focusVisual}"
  Style="{StaticResource button}" />
<ComboBox FocusVisualStyle="{StaticResource focusVisual}" />

The difference will be apparent when the button is given focus. The button should appear green and if focused will show a red focus rectangle. If the combo box gets focus, it will have a similar red rectangle.

当按钮获得焦点时,差异将很明显。该按钮应显示为绿色,如果获得焦点将显示一个红色的焦点矩形。如果组合框获得焦点,它将有一个类似的红色矩形。

While the button style can only be used for Button elements, the focusVisual style can be used for any element that supports it. This way a consistent focus style can be used for any element, regardless of its underlying control type.

虽然按钮样式只能用于 Button 元素,但 focusVisual 样式可以用于支持它的任何元素。这样,一致的焦点样式可用于任何元素,无论其底层控件类型如何。