tags:

views:

380

answers:

2

I have a static ArrayList of DateTimes

<Window.Resources>
    <col:ArrayList x:Key="startHours">
        <sys:DateTime>0:00</sys:DateTime>
        <sys:DateTime>0:30</sys:DateTime>
        <!-- .... -->
    </col:ArrayList>
</Window.Resources>

I want to fill a combobox with hour:minute formatted dates, so I do

<ComboBox x:Name="cmbFinish" 
          ItemsSource="{Binding Source={StaticResource startHours}}" 
          ItemStringFormat="t"/>

The Items are formatted ok - hour:minute, but when I select an item, the cmbFinish.Value has the default DateTime.ToString() format. What am I missing? I need the cmbFinish.Value to be in hour:minute format.

+1  A: 

I don't see a Value property in ComboBox class. Were you thinking about the SelectedValue property? In this case cast its value to a DateTime (DateTime)cmbFinish.SelectedValue that you can then convert to the string format you want.

Mart
Thank you for your answer, you are right, I mean cmvFinish.SelectedValue. I'm sorry if I didn't made myself clear, but I don't get what you mean by (DateTime)cmbFinish.SelectedValue and convert it..
Svetlozar Angelov
If your combobox is editable, the SelectedValue property may contain a DateTime or null. You have to check it before casting the SelectedValue into a DateTime. In both cases, you can read the ComboBox.Text value to read the typed text.
Mart
A: 

I'm sorry... My combobox has a IsEditable(I forgot to add it into the question) property set to True. According to social msdn in this case .SelectedValue will pick up the source value, even if it has a converter applied.

If the IsEditable property is set to False, there is no difference in the Items format and .SelectedValue format. Right now I think the best way is to bind to a converted into strings ArrayList.

Svetlozar Angelov