Well this is driving me nuts, I'm sure this is a simple thing but I can't
see it.
I've got a combobox with a list of items set in the xaml. like so;

<ComboBox x:Name="cmbActivity"
SelectionChanged="cmbActivity_SelectionChanged" >
    <ComboBoxItem />
    <ComboBoxItem Content="Walking"/>
    <ComboBoxItem Content="Running"/>
    <ComboBoxItem Content="Hiking"/>
    <ComboBoxItem Content="Treadmill"/>
    <ComboBoxItem Content="Swimming"/>
</ComboBox>

its on a user control which is in a DataTemplate on a listbox, so the
DataContext is set via the listbox item.

<ListBox x:Name="lstActivities"
ItemTemplate="{StaticResource cardioProgramListItemDataTemplate}" />

<DataTemplate x:Key="cardioProgramListItemDataTemplate">

 <ExerciseLibrary:CardioProgramListItem></ExerciseLibrary:CardioProgramListItem>
</DataTemplate>

with the ComboBox xaml above living in the CardioProgramListItem user
control.

I've tried databinding the combobox's ItemSource to the underlaying
DataContext but that's not right, that's where it gets the items for the
dropdown, not where it puts the selected value.
So my selectionChanged method does this;

CardioActivityData cardioActivityData = this.DataContext as
CardioActivityData;
if (cardioActivityData != null) {
    cardioActivityData.Description = ((ComboBoxItem) ((ComboBox)
sender).SelectedItem).Content.ToString();
}

which works fine, but what I'm not getting is how do I Set the value in the
drop down?
I've tried this on the loaded event for the control to set the selectedItem
of the combobox to the value in the DataContext

var displayText = ((CardioActivityData) DataContext).Description;
cmbActivity.SelectedIndex = cmbActivity.Items.IndexOf(displayText);

but the control loads before the lstActivities ItemSource is assigned so it
happens too early. There's no public DataContext changed event and when I
assign the ItemSource for the listbox looping through the Items in the
listbox returns the underlaying Data, not the user control.

Hope that's clear enough to understand. It's probably something insanely
simple. :)

cheers,
Stephen
--------------------------------------------------------------------------------
Support procedure: https://www.codify.com/lists/support
List address: [email protected]
Subscribe: [email protected]
Unsubscribe: [email protected]
List FAQ: http://www.codify.com/lists/ozsilverlight
Other lists you might want to join: http://www.codify.com/lists

Reply via email to