0
votes

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?

1
Your ` AncestorType={x:Type local:MainWindow}` is most likely of type Window instead of MainWindowlokusking

1 Answers

0
votes

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>