【问题标题】:Binding combo box to itemsource from main window resource property从主窗口资源属性将组合框绑定到 itemsource
【发布时间】:2016-06-30 10:39:32
【问题描述】:

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

<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.Data 错误:40:BindingExpression 路径错误:

在“对象”“主窗口”上找不到“StudentStudyPointsList”属性 (名称='')'。 BindingExpression:Path=StudentStudyPointsList;

DataItem='MainWindow' (Name='');目标元素是“组合框” (名称='');目标属性是“ItemsSource”(类型“IEnumerable”)

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

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

主窗口有这个DataContext:

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

如何让 Combo 正确绑定 itemsource?

【问题讨论】:

  • 你的`AncestorType={x:Type local:MainWindow}`很可能是Window而不是MainWindow

标签: c# wpf xaml


【解决方案1】:

我需要:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2021-04-17
    • 1970-01-01
    相关资源
    最近更新 更多