将组合框从主窗口资源属性绑定到itemsource

本文关键字:属性 绑定 itemsource 资源 窗口 组合 | 更新日期: 2023-09-27 17:53:16

我正在尝试绑定这个组合:

<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>

错误:

System.Windows。数据错误:40:BindingExpression路径错误:

在" MainWindow "对象上找不到" StudentStudyPointsList "属性(Name = ")"。BindingExpression:路径= StudentStudyPointsList;

DataItem ="主窗口"(Name = ");目标元素是"ComboBox"(Name = ");目标属性为"ItemsSource"(类型为"IEnumerable")

属性在主视图模型中公开定义:

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

主窗口有这个DataContext:

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

如何让Combo正确绑定项目源?

将组合框从主窗口资源属性绑定到itemsource

I need:

<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>