title: Object created in XAML can't be called by x:Name in cs
body: <p>The following code represents a simple project in which I am trying to call objects created in XAML. So, I call them by <code>x:Name</code>. In the test UI I created a grid with three rows. The second and third row are being populated directly by Label2 and Label3, but the first row is populated indirectly, via <code>Window.Resources</code>.</p>
  
  <p>The problem is that <code>myLabel2</code> and <code>myLabel3</code> are recognizable in MainWindow.xaml.cs (the AutoComplete works for them), but for <code>myLabel1</code> I receive en error message:</p>
  
  <p><code>The name 'myLabel1' does not exist in the current project</code></p>
  
  <p><strong>So, I guess that to problem derives from the fact that <code>myLabel1</code> was created inside <code>Window.Resources</code>.</strong> </p>
  
  <p>Any thoughts would be appreciated.</p>
  
  <p><strong>This is the MainWindow.xaml code:</strong></p>
  
  <pre><code> &lt;Window x:Class="test2.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:test2"
          mc:Ignorable="d"
          Title="MainWindow" Height="450" Width="800"&gt;
  
      &lt;Window.Resources&gt;
          &lt;DataTemplate x:Key="something"&gt;
              &lt;Label x:Name="myLabel1" Content="Labe1"/&gt;
          &lt;/DataTemplate&gt;
      &lt;/Window.Resources&gt;
  
          &lt;Grid&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="*"/&gt;
                  &lt;RowDefinition Height="*"/&gt;
                  &lt;RowDefinition Height="*"/&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;ContentPresenter Grid.Row="0" ContentTemplate="{StaticResource something}" /&gt;
              &lt;Label x:Name="myLabel2" Content="Labe2" Grid.Row="1" /&gt;
              &lt;Label x:Name="myLabel3" Content="Labe3" Grid.Row="2" /&gt;
          &lt;/Grid&gt;
  
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>This is the MainWindow.xaml.cs code:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace test2
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              myLabel2.Content = "new content myLabel2";
              myLabel3.Content = "new content myLabel3";
              myLabel1.Content = "new content myLabel1";
          }
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  Could I possibly use in .cs something like <code>this.Resources["something"]</code> ?</p>
  
  <p>That was loosely adaptive from <a href="https://stackoverflow.com/questions/3308868/how-to-define-and-use-resources-in-xaml-so-they-can-be-used-in-c-sharp">How to define and use resources in xaml so they can be used in C#</a></p>