title: Exposing inner Control properties for binding in WPF
body: <p><em>[Edit]:  I figured out how to do this on my own. I posted my solution in the hope that it will save someone else a few days of Googling. If you are a WPF guru, please look at my solution and let me know if there is a better / more elegant / more efficient way to do this.  In particular, I am interested in knowing what I don't know... how is this solution going to screw me down the road? The problem really boils down to exposing inner control properties.</em></p>
  
  <p>Problem:
  I am creating some code to auto-generate a data-bound GUI in WPF for an XML file. I have an xsd file that can help me determine the node types, etc. Simple Key/Value elements are easy.</p>
  
  <p>When I parse this element:</p>
  
  <pre><code>&lt;Key&gt;value&lt;/Key&gt;
  </code></pre>
  
  <p>I can create a new 'KeyValueControl' and set the <code>DataContext</code> to this element. The <code>KeyValueControl</code> is defined as a <code>UserControl</code> and just has some simple bindings on it. It works great for any simple XElement.</p>
  
  <p>The XAML inside this control looks like this:</p>
  
  <pre><code>&lt;Label Content={Binding Path=Name} /&gt; 
  &lt;TextBox Text={Binding Path=Value} /&gt;
  </code></pre>
  
  <p>The result is a line that has a label with the element name and a text box with the value that I can edit.</p>
  
  <p>Now, there are times where I need to display lookup values instead of the actual value. I would like to create a 'KeyValueComboBox' similar to the above <code>KeyValueControl</code> but be able to specify (based on information in the file) the <code>ItemsSource</code>, <code>DisplayMemberPath</code>, and <code>ValueMemberPath</code>.  The 'DisplayMemberPath' and 'ValueMemberPath' bindings would be the same as the <code>KeyValueControl</code>.</p>
  
  <p>I don't know if a standard user control can handle this, or if I need to inherit from <code>Selector</code>.  </p>
  
  <p>The XAML in the control would look something like this:</p>
  
  <pre><code>&lt;Label Content={Binding Path=Name} /&gt; 
  &lt;ComboBox SelectedValue={Binding Path=Value}
            ItemsSource={Binding [BOUND TO THE ItemsSource PROPERTY OF THIS CUSTOM CONTROL]
            DisplayMemberPath={Binding [BOUND TO THE DisplayMemberPath OF THIS CUSTOM CONTROL]
            SelectedValuePath={Binding [BOUND TO THE SelectedValuePath OF THIS CUSTOM CONTROL]/&gt;
  </code></pre>
  
  <p>In my code, I would then do something like this (assuming that this node is a 'Thing' and needs to display a list of Things so the user can select the ID:</p>
  
  <pre><code>var myBoundComboBox = new KeyValueComboBox();
  myBoundComboBox.ItemsSource = getThingsList();
  myBoundComboBox.DisplayMemberPath = "ThingName";
  myBoundComboBox.ValueMemberPath = "ThingID"
  myBoundComboBox.DataContext = thisXElement;
  ...
  myStackPanel.Children.Add(myBoundComboBox)
  </code></pre>
  
  <p>So my questions are:</p>
  
  <p>1) Should I inherit my <code>KeyValueComboBox</code> from <code>Control</code> or <code>Selector</code>?</p>
  
  <p>2) If I should inherit from <code>Control</code>, how do I expose the inner Combo Box's <code>ItemsSource</code>, <code>DisplayMemberPath</code>, and <code>ValueMemberPath</code> for binding?</p>
  
  <p>3) If I need to inherit from Selector, can someone provide a small example of how I might get started with that?  Again, I'm new to WPF so a nice, simple example would really help if that's the road I need to take.</p>