如何绑定ItemSource之外的DataGridComboBoxColumn [英] How to bind a DataGridComboBoxColumn outside the ItemSource

查看:140
本文介绍了如何绑定ItemSource之外的DataGridComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个生产WPF应用程序,我遇到了一个问题。

我有一些类似于下面的例子的代码。我遇到的问题是我无法获取 ComboBox 来填充。我假设是因为 Grid ItemsSource 是'阻止'code> ComboBox 可以看到ViewModel上的Tasks集合,但我只是猜测。其他一切都是正确的数据绑定。



我搜索了SO,发现这个问题;这听起来就像我想要做的,但它对我来说不起作用。



任何想法为什么我不能得到 ComboBox



型号:

  public class Activity {
public int Id {get;组; }
public string标题{get;组; }
public Task Task {get;组; }
}

public class Task {
public int Id {get;组; }
public string标题{get;组; }
}

ViewModel:

  public ApprovalViewModel {
public ObservableCollection< Activity>活动{/ * ...属性逻辑* /}
public ObservableCollection< Task>任务{/ * ...属性逻辑* /}
}

查看: p>

 < 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}<! - 我认为问题在这里 - >
SelectedValue ={Binding Task}
DisplayMemberPath =Title/>
< / DataTemplate>
< /DataGrid.Resources>
< DataGrid.Columns>
< DataGridComboBoxColumn DisplayMemberPath =标题/>
< DataGridTextColumn Binding ={Binding User}Header =User/>
< DataGridTextColumn Binding ={Binding Task.Project.Title}Header =Project/>
< DataGridTemplateColumn
标题=任务
CellTemplate ={StaticResource displayTemplate}
CellEditingTemplate ={StaticResource editTemplate}/>
< DataGridTextColumn Binding ={Binding Description}Header =Description/>
< /DataGrid.Columns>
< / DataGrid>

编辑:正确的 ComboBox 代码在这里:

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


解决方案

你在问题​​所在的地方是正确的,相对于活动。所以它正在寻找 Activity.Tasks



你链接的帖子有正确的方法,你只需要调整你的情况。

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

这将返回Visual tree寻找窗口,它有一个 DataContext.Tasks 属性。



你的代码示例在窗口中?如果不是,您将需要更改{x:Type},其次是在此对象上设置的 DataContext 如果不是,您将需要将其设置为您的ViewModel。


I am working on my first 'production' WPF app, and I'm encountering an issue.
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. I'm assuming it is because the Grid's ItemsSource is 'blocking' the ComboBoxfrom being able to see the Tasks collection on the ViewModel, but I am only guessing. Everything else is databinding correctly.

I scoured SO and found this question; 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?

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 
  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. So it is looking for 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.

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? If not you will need to set it to your ViewModel.

这篇关于如何绑定ItemSource之外的DataGridComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆