Binding a ComboBox's ItemSource when in an ItemsControl DataTemplate - best way?
-
I have an ItemsControl bound to a collection within my view model, and a data template to display a data-entry form. Any suggestions on how I should bind the combobox to a property on the original data context? Because the ComboBox is part of the DataTemplate in an ItemTemplate, the context is set to the current item. Code Excerpt:
<ItemsControl ItemsSource="{Binding Source=WorkingCopy.CalendarBasedAttributes.Attributes}"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid Margin="5 20 0 0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> </Grid.RowDefinitions> <Label Content="{Binding CalendarTimePeriod}" Grid.Column="0" Grid.Row="0"/> <ComboBox ItemsSource="{Binding WHATGOESHERE?}" Grid.Column="1" Grid.Row="0" /> </Grid> </DataTemplate> </ItemsControl.ItemTemplate>
-
I have an ItemsControl bound to a collection within my view model, and a data template to display a data-entry form. Any suggestions on how I should bind the combobox to a property on the original data context? Because the ComboBox is part of the DataTemplate in an ItemTemplate, the context is set to the current item. Code Excerpt:
<ItemsControl ItemsSource="{Binding Source=WorkingCopy.CalendarBasedAttributes.Attributes}"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid Margin="5 20 0 0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> </Grid.RowDefinitions> <Label Content="{Binding CalendarTimePeriod}" Grid.Column="0" Grid.Row="0"/> <ComboBox ItemsSource="{Binding WHATGOESHERE?}" Grid.Column="1" Grid.Row="0" /> </Grid> </DataTemplate> </ItemsControl.ItemTemplate>
James J. Foster wrote:
WHATGOESHERE?
Can you post an example of the class you are binding to (the type of objects in the WorkingCopy.CalendarBasedAttributes.Attributes collection)? All I can glean from your posted code is there's a "CalendarTimePeriod" member somewhere :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
James J. Foster wrote:
WHATGOESHERE?
Can you post an example of the class you are binding to (the type of objects in the WorkingCopy.CalendarBasedAttributes.Attributes collection)? All I can glean from your posted code is there's a "CalendarTimePeriod" member somewhere :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I wanted to minimize my dependencies to within the ItemsControl, and not assume a particular relationship between objects in my ViewModel (the data context). Here's at least one solution that works:
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.Stores}"
-
I wanted to minimize my dependencies to within the ItemsControl, and not assume a particular relationship between objects in my ViewModel (the data context). Here's at least one solution that works:
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.Stores}"
That's cool....does it work? :)
Mark Salsbery Microsoft MVP - Visual C++ :java: