如果给定 x:Key,则样式会产生不同的渲染结果
A style gives rise to a different rendering result if given a x:Key
如果给定 x:Key
,样式会产生不同的渲染结果
我有以下样式和 ToggleButtons
的 ControlTemplate
,它们按我想要的方式工作。
<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>
然而,当我给 Style
一个 x:Key
并且 ControlTemplate
中的 RadioButton
继承 Style 时,如下所示,
渲染结果和上面代码给出的不一样
<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>
谁能告诉我为什么会这样?
指定一个时需要使用密钥。
带键的样式不会自动反映到切换按钮上。
MSDN 文档:
Setting the TargetType
property to the RadioButton
type without setting an x:Key
implicitly sets the x:Key
to {x:Type RadioButton }
. This also means that if you give the above Style an x:Key
value of anything other than {x:Type RadioButton}
, the Style would not be applied to all RadioButton
elements automatically. Instead, you need to apply the style to the RadioButton
elements explicitly like "{StaticResource RadioStyle}"
.
如果给定 x:Key
我有以下样式和 ToggleButtons
的 ControlTemplate
,它们按我想要的方式工作。
<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>
然而,当我给 Style
一个 x:Key
并且 ControlTemplate
中的 RadioButton
继承 Style 时,如下所示,
渲染结果和上面代码给出的不一样
<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>
谁能告诉我为什么会这样?
指定一个时需要使用密钥。 带键的样式不会自动反映到切换按钮上。
MSDN 文档:
Setting the
TargetType
property to theRadioButton
type without setting anx:Key
implicitly sets thex:Key
to{x:Type RadioButton }
. This also means that if you give the above Style anx:Key
value of anything other than{x:Type RadioButton}
, the Style would not be applied to allRadioButton
elements automatically. Instead, you need to apply the style to theRadioButton
elements explicitly like"{StaticResource RadioStyle}"
.