views:

195

answers:

1

Hi all,
Let me first describe my goal: I have created an object with 3 properties: start, end and time. I have created an ObservableCollection of 8 of these properties, so it looks like this:

//C#
internal class MyObjects : ObservableCollection<MyObjectSetting>
        {
            public MyObjects()
                : base()
            {
                Add(new MyObjectSetting(
                            start1,
                            end1,
                            time1);
                Add(new MyObjectSetting(
                            start2,
                            end2,
                            time2);
    (etc)
            }
        }

I would like to have 3 ComboBoxes that bind to the individual properties listed in these 8 objects, so ComboBoxes would look like "start1, start2, ... start8", "end1, end2, ... end8".

The following code successfully binds the ComboBox to the objects themselves, but I am stymied as to how to access the individual properties of each of the comboboxes.

// WPF
    <Grid>
        <Grid.Resources>
            <local:MyObjects x:Key="myMyObjects"/>
        </Grid.Resources>

        <ComboBox x:Name="cbxStartPosition" 
                  Grid.Row="0" 
                  Grid.Column="3" 
                  ItemsSource="{Binding Source={StaticResource myMyObjects}}"                  
                  >
    </Grid>

Can someone help me determine how to bind the properties of the objects stored in the collection to the display value shown in the ComboBox?

I've tried adding a DataTemplate for the ListBoxinvestigating the MultiBinding sample on MSDN here as shown below, but receive an error below:

//WPF
        <DataTemplate x:Key="StartPositionTemplate">
            <ListBox>
                <MultiBinding Converter="{StaticResource myNameConverter}">
                    <Binding Path="FirstName"/>
                    <Binding Path="LastName"/>
                </MultiBinding>
            </ListBoxItem>
        </DataTemplate>

Error 32 A value of type 'DataTemplate' cannot be added to a collection or dictionary of type 'UIElementCollection'.


This error was triggered because my was not within the XAML section. HTH people in the future. Using a DataTemplate was the way to go, per the answer below.


If DataTemplate isn't the way to go, does someone know what would be a better way to approach this?

+2  A: 

If you're just trying to display the string value of a property, you can use DisplayMemberPath:

<ComboBox ItemsSource="{Binding Source={StaticResource myMyObjects}}" DisplayMemberPath="Start"/>

For more complex scenarios, you can use a custom item template:

<ComboBox ItemsSource="{Binding Source={StaticResource myMyObjects}}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Start}"/>
                <TextBlock Text="{Binding End}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

HTH, Kent

Kent Boogaart
This is *exactly* what I was looking for. Thank you so very much - I used the second complex scenario, I will dig deeper into ItemTemplates to understand what's going on for future work. Great job!
CrimsonX