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

时间:2016-06-30 10:39:33

标签: c# wpf xaml

我正在尝试绑定此组合:

<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路径错误:

     

&#39; StudentStudyPointsList&#39;在&#39; object&#39;上找不到的属性&#39;&#39;主窗口&#39;    (名称=&#39;&#39;)&#39 ;. BindingExpression:路径= StudentStudyPointsList;

     

的DataItem =&#39;主窗口&#39; (名称=&#39;&#39);目标元素是&#39; ComboBox&#39;    (名称=&#39;&#39);目标属性是&#39; ItemsSource&#39; (键入&#39; IEnumerable&#39;)

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

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

主窗口有这个DataContext:

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

如何让Combo正确绑定itemsource?

1 个答案:

答案 0 :(得分:0)

我需要:

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