title: WPF binding to Listbox selectedItem
body: <p>Can anyone help with the following - been playing about with this but can't for the life of me get it to work.</p>
  
  <p>I've got a view model which contains the following properties;</p>
  
  <pre><code>public ObservableCollection&lt;Rule&gt; Rules { get; set; }
  public Rule SelectedRule { get; set; }
  </code></pre>
  
  <p>In my XAML I've got;</p>
  
  <pre><code>&lt;ListBox x:Name="lbRules" ItemsSource="{Binding Path=Rules}" 
           SelectedItem="{Binding Path=SelectedRule, Mode=TwoWay}"&gt;
  &lt;ListBox.ItemTemplate&gt;
      &lt;DataTemplate&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="Name:" /&gt;
                  &lt;TextBox x:Name="ruleName"&gt;
                      &lt;TextBox.Text&gt;
                          &lt;Binding Path="Name" UpdateSourceTrigger="PropertyChanged" /&gt;
                      &lt;/TextBox.Text&gt;
                  &lt;/TextBox&gt;
              &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
  &lt;/ListBox.ItemTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p>Now the ItemsSource works fine and  I get a list of Rule objects with their names displayed in lbRules. </p>
  
  <p>Trouble I am having is binding the SelectedRule property to lbRules' SelectedItem. I tried binding a textblock's text property to SelectedRule but it is always null.</p>
  
  <pre><code>&lt;TextBlock Text="{Binding Path=SelectedRule.Name}" /&gt;
  </code></pre>
  
  <p>The error I'm seeing in the output window is:
  BindingExpression path error: 'SelectedRule' property not found.</p>
  
  <p>Can anyone help me with this binding - I can't see why it shouldn't find the SelectedRule property.</p>
  
  <p>I then tried changing the textblock's text property as bellow, which works. Trouble is I want to use the SelectedRule in my ViewModel.</p>
  
  <pre><code>&lt;TextBlock Text="{Binding ElementName=lbRules, Path=SelectedItem.Name}" /&gt;
  </code></pre>
  
  <p>Thanks very much for your help.</p>