Andrew Truckle
Andrew Truckle

Reputation: 19197

Binding combo box to itemsource from main window resource property

I am trying to bind this combo:

<ComboBox ItemsSource="{Binding StudentStudyPointsList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Number}"/>
                <TextBlock Text=" - "/>
                <TextBlock Text="{Binding Title}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

The error:

System.Windows.Data Error: 40 : BindingExpression path error:

'StudentStudyPointsList' property not found on 'object' ''MainWindow' (Name='')'. BindingExpression:Path=StudentStudyPointsList;

DataItem='MainWindow' (Name=''); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

The property is publicly defined in the main view model:

public List<StudyPointItem> StudentStudyPointsList { get; set; }

And the main window has this DataContext:

<Window.DataContext>
    <local:OCLMEditorModelView/>
</Window.DataContext>

How do I get the Combo to bind the itemsource correctly?

Upvotes: 0

Views: 540

Answers (1)

Andrew Truckle
Andrew Truckle

Reputation: 19197

I needed:

<ComboBox DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}"
          ItemsSource="{Binding ReadingStudyPointsList}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Number}"/>
                <TextBlock Text=" - "/>
                <TextBlock Text="{Binding Title}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Upvotes: 0

Related Questions