Note that there are some explanatory texts on larger screens.

plurals
  1. POExposing inner Control properties for binding in WPF
    primarykey
    data
    text
    <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 KeyValue is defined as a UserControl 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 KeyValueControl but be able to specify (based on information in the file) the ItemsSource, Display and Value paths. The 'Name' and 'Value' bindings would be the same as the KeyValueControl.</p> <p>I don't know if a standard user control can handle this, or if I need to inherit from Selector. </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 KeyValueComboBox from Control or Selector?</p> <p>2) If I should inherit from Control, how do I expose the inner Combo Box's ItemsSource, DisplayMemberPath, and ValueMemberPath 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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload