简体   繁体   English

如何在ItemSource外部绑定DataGridComboBoxColumn

[英]How to bind a DataGridComboBoxColumn outside the ItemSource

I am working on my first 'production' WPF app, and I'm encountering an issue. 我正在开发我的第一个“生产” WPF应用程序,但是遇到了问题。
I have some code that is similar to the example below. 我有一些类似于以下示例的代码。 The problem I am having is that I cannot get the ComboBox to populate. 我遇到的问题是我无法填充ComboBox I'm assuming it is because the Grid 's ItemsSource is 'blocking' the ComboBox from being able to see the Tasks collection on the ViewModel, but I am only guessing. 我假设这是因为GridItemsSource正在“阻止” ComboBox ,使其无法查看ViewModel上的Tasks集合,但我只是在猜测。 Everything else is databinding correctly. 其他所有数据都正确绑定。

I scoured SO and found this question; 我搜寻了SO,发现了这个问题; which sounds exactly like what I am trying to do, but it did not work for me. 听起来完全像我要执行的操作,但对我却不起作用。

Any idea why I can't get the ComboBox to populate? 知道为什么我无法填充ComboBox吗?

Model: 模型:

public class Activity{
  public int Id { get; set; }
  public string Title { get; set; }
  public Task Task { get; set; }
}

public class Task{
  public int Id { get; set; }
  public string Title { get; set; }
}

ViewModel: 视图模型:

public ApprovalViewModel{
  public ObservableCollection<Activity> Activities { /* ... property logic */ }
  public ObservableCollection<Task> Tasks { /* ... property logic */ }
}

View: 视图:

<DataGrid ItemsSource="{Binding Activities}" AutoGenerateColumns="False">
    <DataGrid.Resources>
        <DataTemplate x:Key="displayTemplate">
            <TextBlock Text="{Binding Task.Title}"/>
        </DataTemplate>
        <DataTemplate x:Key="editTemplate">
            <ComboBox ItemsSource="{Binding Tasks}" <!--I think the problem is here-->
              SelectedValue="{Binding Task}"
              DisplayMemberPath="Title"/>
        </DataTemplate>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridComboBoxColumn DisplayMemberPath="Title"/>
        <DataGridTextColumn Binding="{Binding User}" Header="User"/>
        <DataGridTextColumn Binding="{Binding Task.Project.Title}" Header="Project"/>
        <DataGridTemplateColumn 
            Header="Task" 
            CellTemplate="{StaticResource displayTemplate}" 
            CellEditingTemplate="{StaticResource editTemplate}"/>
        <DataGridTextColumn Binding="{Binding Description}" Header="Description"/>
    </DataGrid.Columns>
</DataGrid>

Edit: the correct ComboBox code is here: 编辑:正确的ComboBox代码在这里:

<ComboBox 
  ItemsSource="{Binding Path=DataContext.Tasks, 
                        RelativeSource={RelativeSource FindAncestor, 
                                        AncestorType={x:Type UserControl}}}"
  SelectedValue="{Binding Task.Title}"
  SelectedValuePath="Title"
  DisplayMemberPath="Title"/>

You are correct in where the problem lies, that binding is relative to the Activity . 您的问题所在是正确的,即绑定是相对于Activity So it is looking for Activity.Tasks . 因此,它正在寻找Activity.Tasks

The post you linked to has the right approach, you just need to tweak it for your situation. 您链接到的帖子具有正确的方法,您只需要针对您的情况进行调整即可。

ItemsSource="{Binding Path=DataContext.Tasks, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 

This will go back up the Visual tree looking for a Window, which has a DataContext.Tasks property. 这将在可视树中返回以查找具有DataContext.Tasks属性的窗口。

Is your code example inside a window? 您的代码示例是否在窗口内? If not you will need to change the {x:Type } and secondly is the DataContext set on this object? 如果不是,则需要更改{x:Type},其次是在该对象上设置DataContext吗? If not you will need to set it to your ViewModel. 如果不是,则需要将其设置为ViewModel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM