Friday, September 16, 2011

...and now for a little Silverlight/XAML. I tend to use combos and listboxes a lot, so I thought it would be good to include this little code snippet. It's a simple datatemplate for a combobox that will use a binding path to reference the KEY field in the KeyValuePairs<> generic. This way, when I set the combobox's ItemsSource to point to an ObservableCollection<keyvaluepairs<>>of whatever type, it only shows the KEY field in the drop down. FYI, you should use an ObservableCollection<> because then you can modify the ItemsSource list at any time even if the combobox is in use and it will auto-update the list for you. So here's what the template looks like:
 <combobox itemssource="{Binding}" x:name="MyComboBox">  
  <combobox.itemtemplate>  
   <datatemplate>  
    <textblock text="{Binding Key}">  
   </textblock></datatemplate>  
  </combobox.itemtemplate>  
 </combobox>  

No comments: