WPF/XAML about the difference between x:key and x:name, comprehensive interpretation super detailed

The difference between x:key and x:name

x:Key x:Name
For xaml Resources, ResourceDictionary Used anywhere other than ResourceDictionary
Use key to access xaml specified object Use name to access xaml object
Identifies resource creation and references, elements present in ResourceDictionary Uniquely identifies object elements for easy access to instantiated elements from code-behind or generic code
Provides a unique identifier for resource files defined in xaml Provides a unique identifier for control elements defined in xaml

x:key case

insert image description here

<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 case

insert image description here

<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>

Guess you like

Origin blog.csdn.net/gao511147456/article/details/128351480