Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sounds like you want to get into the wonderful world of <code>DataTemplates</code></p> <p>first we'll create a container class for your data</p> <pre><code>public class Person { public string FirstName {get; set;} public string LastName {get; set;} public Person(string first, string last) { FirstName = first; LastName = first; } } </code></pre> <p>Then, in the code behind of your hosting element (probably window). You can declare and <code>ObservableCollection&lt;&gt;</code> of the objects.</p> <pre><code>//populate me wherever! public ObservableCollection&lt;Person&gt; People {get;set;} </code></pre> <p>(dont forget to initialize that somewhere to avoid all kinds of unnecessary exceptions)</p> <p>Then you can bind the collection to an items collection, i like the <code>ItemsControl</code> </p> <pre><code>&lt;ItemsControl ItemsSource="{Binding Path=People}"/&gt; </code></pre> <p>Except this doesn't give the result we want. So while we could override the <code>ToString()</code> of our class to spit out some formatted data, we can do one better by overriding the item template of the items control.</p> <pre><code>&lt;ItemsControl ItemsSource="{Binding Path=People}"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Button DataContext="{Binding}" Click="Button_Click"&gt; &lt;Button.Content&gt; &lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding StringFormat="{}{1}, {0}"&gt; &lt;Binding Path="FirstName"/&gt; &lt;Binding Path="LastName"/&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; &lt;/Button.Content&gt; &lt;/Button&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; </code></pre> <p>Note that I'm using a multi-binding with a string formatter in order to format the text correctly on the buttons.</p> <p>Now on the Buttons <code>Click</code> Event handler, you can process an item. As the <code>Sender</code> of the event will be the button, and the <code>DataContext</code> of the button will be the person object it represents and you can do what you wish. </p> <p>You might also find the <code>ListBox</code> and its <code>SelectedItem</code> Property very useful in your particular case. </p> <p>Hopefully this is enough to get you started, -Val</p>
 

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