x:key和x:name的区别
x:Key | x:Name |
---|
用于xaml Resources,ResourceDictionary | 用在ResourceDictionary以外任何地方 |
使用key访问xaml指定对象 | 使用name访问xaml对象 |
标识资源创建和引用,存在于 ResourceDictionary 中的元素 | 唯一标识对象元素,以便于从代码隐藏或通用代码中访问实例化的元素 |
为xaml中定义的资源文件提供唯一的标识 | 为xaml中定义的控件元素提供唯一标识 |
x:key案列
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.RadioButton.xaml" />
</ResourceDictionary.MergedDictionaries>
<converters:ColorToBrushConverter x:Key="ColorToBrushConverter" />
<Style x:Key="HexLabelTextBlock"
TargetType="TextBlock"
BasedOn="{StaticResource MaterialDesignBody2TextBlock}">
<Setter Property="Margin" Value="8" />
</Style>
<DataTemplate x:Key="SwatchColorTemplate" DataType="{x:Type Color}">
</DataTemplate>
</ResourceDictionary>
x:name案列
<UniformGrid Columns="1" DockPanel.Dock="Right">
<RadioButton x:Name="MdPaletteButton"
Margin="4"
Content="MD Palette"
IsChecked="True"
Style="{StaticResource MaterialDesignTabRadioButton}" />
<RadioButton x:Name="CustomPaletteButton"
Margin="4"
Content="Custom"
IsChecked="False"
Style="{StaticResource MaterialDesignTabRadioButton}" />
</UniformGrid>