title: wpf dynamic window
body: <p>I am trying to create a window to act as a switch board for testing an application I'm working on, and having two problems doing so.</p>
  
  <p>The first problem is trying to set the size &amp; margin of buttons that need to be added to a stack panel dynamically. In XAML, the properties look like this:</p>
  
  <pre><code>&lt;StackPanel Name="_mainStackPanel" &gt;
      &lt;Button Height="Auto" Width="Auto" Margin="10,10,10,5" &gt;Do Something&lt;/Button&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>The second problem is trickier. I want the switch board window to have a status bar with an exposed property, like so:</p>
  
  <pre><code>&lt;StatusBar BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Left"&gt;
      &lt;TextBlock Name="_statusBar" Foreground="Black" TextWrapping="Wrap"&gt;blah&lt;/TextBlock&gt;
  &lt;/StatusBar&gt;
  
  public string Status {
          get { return _statusBar.Text; } 
          set { _statusBar.Text = value; }
  }
  </code></pre>
  
  <p>In a test case I'm working through, I just want each button to display it's text content in the status bar when it's clicked. Invoking the click gets a NullReferenceException...</p>
  
  <p>I'm using the command pattern to tell the button what to do when it's clicked. Here's the code in the switch board window where commands are turned into buttons:</p>
  
  <pre><code>private void _addButtons() {
          foreach (var cmd in _commands)
          {
              var b = new Button
                      {
                          Content = cmd.DisplayText,
                          // height = "Auto" double.NaN ?
                          // width = "Auto"
                          // margin = "10,10,10,5
                      };
              var command = cmd;
              b.Click += ((sender, args) =&gt; command.Execute());
              _mainStackPanel.Children.Add(b);
          }
      }
  </code></pre>
  
  <p>And here is the setup in my "test" (I am not asserting anything, just firing up the gui and seeing if it works):</p>
  
  <pre><code>[TestFixture]
  public class SwitchBoardTests
  {
      private SwitchBoardView _switchboard;
      private Application _app;
  
      [SetUp]
      public void SetUp() {
          var commands = new List&lt;IDisplayableCommand&gt; {
                                                           new StatusCommand("Hello...", _switchboard),
                                                           new StatusCommand("Good Bye...", _switchboard),
                                                       };
          _switchboard = new SwitchBoardView(commands);
      }
  
      class StatusCommand : DisplayCommand
      {
          private readonly SwitchBoardView _view;
          public StatusCommand(string message, SwitchBoardView view) : base(message) {
              _view = view;
          }
          public override void Execute() { _view.Status = DisplayText; }
      }
  
      [Test]
      public void Display() {
          _app = new Application();
          _app.Run(_switchboard);
          //_switchboard.Show();
      }
  
  }
  </code></pre>
  
  <p>I am just learning WPF, so any best practices that might come out of solving these two issues would be most welcome!</p>
  
  <p>Cheers,<br>
  Berryl</p>