C# 如果给定x:Key,则样式会产生不同的渲染结果

C# 如果给定x:Key,则样式会产生不同的渲染结果,c#,wpf,controltemplate,C#,Wpf,Controltemplate,如果给定x:Key 我有以下样式和ControlTemplate用于ToggleButtons,它们可以按照我的要求工作 <Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}"/> <ControlTemplate TargetType="RadioButton" x:Key="RadioTemplate"> <RadioButton IsChecked

如果给定
x:Key

我有以下样式和
ControlTemplate
用于
ToggleButtons
,它们可以按照我的要求工作

<Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}"/>
<ControlTemplate TargetType="RadioButton" x:Key="RadioTemplate">
    <RadioButton IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked, Mode=TwoWay}">
        <RadioButton.Content>
            <Viewbox StretchDirection="DownOnly" Stretch="Uniform">
                <ContentControl Content="{TemplateBinding Content}"/>
            </Viewbox>
        </RadioButton.Content>
    </RadioButton>
</ControlTemplate>

有人能告诉我为什么会发生这种情况吗?

指定密钥时需要使用密钥。
带有键的样式不会自动反映到切换按钮。

指定键时需要使用该键。
带有键的样式不会自动反映到切换按钮。

指定键时需要使用该键。
带有键的样式不会自动反映到切换按钮。

指定键时需要使用该键。 带有键的样式不会自动反映到切换按钮。

文档:

TargetType
属性设置为
RadioButton
类型而不设置
x:Key
会隐式地将
x:Key
设置为
{x:type RadioButton}
。这也意味着,如果为上述样式指定了除
{x:Type RadioButton}
以外的任何值,则该样式不会自动应用于所有
RadioButton
元素。相反,您需要将样式显式地应用于
RadioButton
元素,如
“{StaticResource RadioStyle}”

文件:

TargetType
属性设置为
RadioButton
类型而不设置
x:Key
会隐式地将
x:Key
设置为
{x:type RadioButton}
。这也意味着,如果为上述样式指定了除
{x:Type RadioButton}
以外的任何值,则该样式不会自动应用于所有
RadioButton
元素。相反,您需要将样式显式地应用于
RadioButton
元素,如
“{StaticResource RadioStyle}”

文件:

TargetType
属性设置为
RadioButton
类型而不设置
x:Key
会隐式地将
x:Key
设置为
{x:type RadioButton}
。这也意味着,如果为上述样式指定了除
{x:Type RadioButton}
以外的任何值,则该样式不会自动应用于所有
RadioButton
元素。相反,您需要将样式显式地应用于
RadioButton
元素,如
“{StaticResource RadioStyle}”

文件:

TargetType
属性设置为
RadioButton
类型而不设置
x:Key
会隐式地将
x:Key
设置为
{x:type RadioButton}
。这也意味着,如果为上述样式指定了除
{x:Type RadioButton}
以外的任何值,则该样式不会自动应用于所有
RadioButton
元素。相反,您需要将样式显式地应用于
RadioButton
元素,如
“{StaticResource RadioStyle}”


指定键时,只需通过该键应用样式,因此无论在何处(togglebuttons)使用样式,都需要像“{StaticResource RadioStyle}”一样访问它。在ControlTemplate中的RadioButton中,我编写了style=“{StaticResource RadioStyle}”。我想知道为什么这还不够,我完全理解。如果我给RadioButton样式指定了一个x:Key,那么即使ControlTemplate中的RadioButton继承了RadioButton样式,我也必须将其用作
。非常感谢您的建议。当您指定一个键时,您只需要通过该键应用样式,因此无论在何处(togglebuttons)使用样式,您都需要像“{StaticResource RadioStyle}”一样访问它。在ControlTemplate内的RadioButton中,我编写了style=“{StaticResource RadioStyle}”。我想知道为什么这还不够,我完全理解。如果我给RadioButton样式指定了一个x:Key,那么即使ControlTemplate中的RadioButton继承了RadioButton样式,我也必须将其用作
。非常感谢您的建议。当您指定一个键时,您只需要通过该键应用样式,因此无论在何处(togglebuttons)使用样式,您都需要像“{StaticResource RadioStyle}”一样访问它。在ControlTemplate内的RadioButton中,我编写了style=“{StaticResource RadioStyle}”。我想知道为什么这还不够,我完全理解。如果我给RadioButton样式指定了一个x:Key,那么即使ControlTemplate中的RadioButton继承了RadioButton样式,我也必须将其用作
。非常感谢您的建议。当您指定一个键时,您只需要通过该键应用样式,因此无论在何处(togglebuttons)使用样式,您都需要像“{StaticResource RadioStyle}”一样访问它。在ControlTemplate内的RadioButton中,我编写了style=“{StaticResource RadioStyle}”。我想知道为什么这还不够,我完全理解。如果我给RadioButton样式指定了一个x:Key,那么即使ControlTemplate中的RadioButton继承了RadioButton样式,我也必须将其用作
。非常感谢你的建议。
<Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}" x:Key="RadioStyle"/>
<ControlTemplate TargetType="RadioButton" x:Key="RadioTemplate">
    <RadioButton Style="{StaticResource RadioStyle}"
                 IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked, Mode=TwoWay}">
        <RadioButton.Content>
            <Viewbox StretchDirection="DownOnly" Stretch="Uniform">
                <ContentControl Content="{TemplateBinding Content}"/>
            </Viewbox>
        </RadioButton.Content>
    </RadioButton>
</ControlTemplate>