Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set silverlight comboboxitem value in xaml

I create a datatemplate for a combobox as follows:

<DataTemplate x:Key="AircraftTypeTemplate">
     <StackPanel Orientation="Horizontal" Width="340">
           <ComboBox>
               <ComboBoxItem>CJ1</ComboBoxItem>
               <ComboBoxItem>CJ3</ComboBoxItem>
               <ComboBoxItem>Bravo</ComboBoxItem>
               <ComboBoxItem>Excel</ComboBoxItem>
               <ComboBoxItem>Sovereign</ComboBoxItem>
           </ComboBox>
     </StackPanel>
</DataTemplate>

It renders fine, but I would like to be able to associate a value with each of the items without having to bind it to some data context. For example I would like the CJ1 comboboxitem to have a value of 5. How would I set those in XAML?

Like:

<ComboBoxItem Value="5">CJ1</ComboBoxItem>

Thanks!

like image 801
Rick Hodder Avatar asked Dec 28 '22 12:12

Rick Hodder


1 Answers

You can set the Name property to be any arbitrary string and use that. For more flexibility, you can use the Tag property, which according to MSDN:

Gets or sets an arbitrary object value that can be used to store custom information about this object.

You can read more about Tag here. I'd say Tag is probably better as opposed to bending Name to your will, and you can stick a string into Tag just as easily as Name.

like image 186
Zann Anderson Avatar answered Jan 02 '23 20:01

Zann Anderson