Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code> x:Key </code></pre> <p>Is used when you need to define an element in a ResourceDictionary . </p> <p>For example : a Style or some element you wan't to initalize in xaml .</p> <pre><code> &lt;Style TargetType=Button x:Key="MyBtnStyle&gt; ...... &lt;myLocalNs:MyType x:Key="MyType1" /&gt; </code></pre> <p>.</p> <pre><code> x:Name </code></pre> <p>Would be used when you wan't to eather reference an element from Your Code Behind :</p> <pre><code> &lt;UserControl&gt; &lt;Grid x:Name="grid"&gt; &lt;Button x:Name="btn" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>in the UserControl.cs :</p> <pre><code> btn.Contnent = "Im A Button"; grid.RowDefenitions.Add(new RowDefenition()); </code></pre> <p>another case where you would use x:Name is when targeting an element in a ControlTemplate / DataTemplate / Style through Triggers , using <strong>TargetName</strong> </p> <p>For Example :</p> <pre><code>&lt;DataTemplate DataType="{x:Type h:HierarchicalNode}"&gt; &lt;Grid&gt; &lt;CheckBox x:Name="chkbox" /&gt; &lt;/Grid&gt; &lt;DataTemplate.Triggers&gt; &lt;DataTrigger Binding="{Binding HasChildren}" Value="False"&gt; &lt;Setter Property="Visibility" Value="Collapsed" TargetName="chkbox"/&gt; &lt;/DataTrigger&gt; &lt;/DataTemplate.Triggers&gt; &lt;/DataTemplate&gt; </code></pre> <p>Another case is when you would wan't to bind to an element in XAML .</p> <pre><code> &lt;UserControl&gt; &lt;Grid x:Name="grid"&gt; &lt;Button x:Name="btn" Content="{Binding ElementName=grid ,Path=Name}"/&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>FYI , x:Name would also be used when you want to Target elements using Interaction Triggers or VisualState's .</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload