title: Visibility Binding using DependencyProperty
body: <p>I've some simple code below that uses a ToggleButton.IsChecked property to set the Visibility of a TextBlock.  It works fine.  Since this doesn't quite fit in with my program's structure, I'm trying to bind the visibility of another TextBlock to a DependencyProperty of "this".  It compiles fine, but it produces no effect.  I'm doing something wrong, just not sure what.</p>
  
  <p>XAML</p>
  
  <pre><code>&lt;Window x:Class="ToggleButtonTest.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Width="200" Height="100"&gt;
  &lt;Window.Resources&gt;
      &lt;BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/&gt;
  &lt;/Window.Resources&gt;
  &lt;StackPanel&gt;
      &lt;ToggleButton x:Name="toggleButton" Content="Toggle"
                    IsChecked="True" Checked="toggleButton_Checked"/&gt;
      &lt;TextBlock Text="Some Text"
                 Visibility="{Binding IsChecked, 
                 ElementName=toggleButton,
                 Converter={StaticResource BooleanToVisibilityConverter}}"/&gt;
      &lt;TextBlock Text="More Text"
                 Visibility="{Binding ShowMoreText, 
                 ElementName=this, 
                 Converter={StaticResource BooleanToVisibilityConverter}}"/&gt;
  &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>using System.Windows;
  
  namespace ToggleButtonTest
  {
      public partial class MainWindow : Window
      {
          static MainWindow()
          {
              FrameworkPropertyMetadata meta = 
                  new FrameworkPropertyMetadata(true,
                  FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
  
              ShowMoreTextProperty = 
                  DependencyProperty.Register("ShowMoreText", 
                  typeof(bool), typeof(MainWindow), meta);
          }
  
          public MainWindow()
          {
              InitializeComponent();
          }
  
          public static readonly DependencyProperty ShowMoreTextProperty;
          public bool ShowMoreText
          {
              get
              {
                  return (bool)GetValue(ShowMoreTextProperty);
              }
              set
              {
                  SetValue(ShowMoreTextProperty, value);
              }
          }
  
          private void toggleButton_Checked(object sender, RoutedEventArgs e)
          {
              ShowMoreText = toggleButton.IsChecked.Value;
          }
      }
  }
  </code></pre>
  
  <p>Edit:  </p>
  
  <p>Having had this answered, I want to post my working code...</p>
  
  <p>XAML</p>
  
  <pre><code>&lt;Window x:Class="ToggleButtonTest.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Width="200" Height="100"
      Name="thisWindow"&gt;
      &lt;Window.Resources&gt;
          &lt;BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/&gt;
      &lt;/Window.Resources&gt;
      &lt;StackPanel&gt;
          &lt;ToggleButton x:Name="toggleButton" 
                  Content="Toggle"
                  IsChecked="{Binding Path=ShowMoreText, ElementName=thisWindow}"/&gt;
          &lt;TextBlock Text="More Text"
                     Visibility="{Binding Path=ShowMoreText, 
                     ElementName=thisWindow,
                     Converter={StaticResource BooleanToVisibilityConverter}}"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>using System.Windows;
  
  namespace ToggleButtonTest
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          public static readonly DependencyProperty ShowMoreTextProperty =
              DependencyProperty.Register("ShowMoreText", typeof(bool),
              typeof(MainWindow), new FrameworkPropertyMetadata(true,
                  FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
  
          public bool ShowMoreText
          {
              get
              {
                  return (bool)GetValue(ShowMoreTextProperty);
              }
              set
              {
                  SetValue(ShowMoreTextProperty, value);
              }
          }
      }
  }
  </code></pre>