title: WPF C# - Making a stack panel inside user control visible from parent
body: <p>So I've seen similar questions asked on here, but haven't been able to nail this down.  We have a page that contains a UserControl where a StackPanel inside it is hidden onload, and the parent has a button that needs to make a StackPanel (<code>stkSomePanel</code> in UserControl1.xaml) inside the UserControl visible when initially clicked (we want to hide it, onload - the code has most of the values as <code>Visible</code>, for now, so we can see it and try to hide it - part of the problem is knowing where to put <code>.Hidden</code> and <code>.Visible</code>, too).  The button text must change from "Edit" to "Save".  When clicked again, the visibility of the StackPanel needs to toggle back to hidden and the text back to "Edit".  </p>
  
  <p>Should be a simple concept, but not clear how to bind what to what.  I have a button on the parent that I'm trying to use to click a child button I'd intend to hide, but not sure I even need the child button.  I've tested variations of this where I could click the child button and it would update the button text of the child button and the visibility of the child StackPanel one time, indicating it runs <code>ClickHereExecuted()</code>, but that's just a one-time set, not a toggle, and nothing happens at all if I click the parent button, which is really what needs to work.</p>
  
  <p>So far, MainWindow.xaml:</p>
  
  <pre><code>&lt;Window x:Class="MyProject.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:src="clr-namespace:MyProject"
      xmlns:v="clr-namespace:MyProject.UserControls"
      Title="MainWindow" Height="350" Width="525"
  &gt;
      &lt;StackPanel&gt;
          &lt;v:GreatUserControl x:Name="UC1" /&gt;
          &lt;Button Content="{Binding ButtonContent}" Command="{Binding ClickHereCommand}"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindow.xaml.cs:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public static RoutedCommand ClickHereCommand {get; set;}
  
      public MainWindow()
      {
          InitializeComponent();
      }
  }
  </code></pre>
  
  <p>UserControl1.xaml:</p>
  
  <pre><code>&lt;UserControl 
      x:Class="MyProject.GreatUserControl"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      &gt;
      &lt;UserControl.Resources&gt;
          &lt;BooleanToVisibiltyConverter x:Key="ConvBoolToVis"/&gt;
      &lt;/UserControl.Resources&gt;
      &lt;Button Content="{Binding ButtonContent}" Command="{Binding ClickHereCommand}" /&gt;
      &lt;StackPanel x:Name="stkSomePanel" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" Visibility="{Binding vis, ElementName=UserControl1, Converter={StaticResource ConvBoolToVis}}"&gt;
      &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>UserControl1.xaml.cs:</p>
  
  <pre><code>namespace MyProject.UserControls
  {
      public partial class UserControl1 : UserControl, INotifyPropertyChanged
      {
          public Visibility vis
          {
              get { return (Visibility)GetValue(VisibilityProperty); }
              set { SetValue(VisibilityProperty, value); }
          }
  
          public static readonly DependencyProperty VisiblityProperty = 
              DependencyProperty.Register("vis", typeof(Visibility), typeof(UserControl1), new UIPropertyMetadata(Visibility.Visible));
  
          public string ButtonContent
          {
              get { return (string)GetValue(ButtonContentProperty); }
              set { SetValue(ButtonContentProperty, value); }
          }
  
          public static readonly DependencyProperty ButtonContentProperty =
              DependencyProperty.Register("ButtonContent", typeof(string), typeof(UserControl1), new UIPropertyMetadata(string.Empty));
  
          public RoutedCommand ClickHereCommand
          {
              get { return (RoutedCommand)GetValue(ClickHereCommandProperty); }
              set { SetValue(ClickHereCommandProperty, value); }
          }
  
          public static readonly DependencyProperty ClickHereCommandProperty =
              DependencyProperty.Register("ClickHereCommand", typeof(RoutedCommand), typeof(UserControl1), new UIPropertyMetadata(null));
  
          public UserControl1()
          {
              InitializeComponent();
  
              ClickHereCommand = new RoutedCommand();
              CommandBindings.Add(new CommandBinding(ClickHereCommand, ClickHereExecuted));            
              ButtonContent = "Edit";
          }
  
          public void ClickHereExecuted(object sender, ExecutedRoutedEventArgs e)
          {
              ButtonContent = "Save";
              vis = Visibility.Visible;
          }
  
  
          #region INotifyPropertyChanged Members
          public event PropertyChangedEventHandler PropertyChanged;
  
          public void OnPropertyChanged(string name)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(name));
              }
          }
          #endregion
      }
  }
  </code></pre>