如何从特定行的Combobox Column中选择?

时间:2014-01-03 11:47:39

标签: c# wpf datagridcomboboxcolumn

我不知道如何在选择后从ComboboxColumn单元正确返回值。我的ComboboxColum看起来像这样:

<DataGridComboBoxColumn CanUserSort="False" 
                        x:Name="cbcList" 
                        DisplayMemberPath="{Binding Person, Converter={StaticResource converter}}" 
                        Header="Person"/>

ItemsSource在后​​面的代码中设置为Person个对象的列表。现在,我如何获取此列中所有选项的Person个对象列表?我尝试制作Person obejcts的新列表并使用SelectedItemBinding绑定它,但是这样做或者我犯了一些错误。

1 个答案:

答案 0 :(得分:1)

SelectedItem没有属性设置器,这意味着您无法为其分配绑定。但是,我们可以通过SelectionChanged事件跟踪多个选择。

假设:您在ViewModel中有一个名为People的ObservableCollection。

在ViewModel中创建一个名为SelectedPeople的Collection属性

Public Collection<Person> SelectedPeople { get; set; }

在XAML中实现SelectionChanged事件。

<DataGrid ItemsSource="{Binding Path=People}" SelectionChanged="Selector_OnSelectionChanged" />

后面代码中的SelectionChanged处理程序看起来像这样。

private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems != null)
    {
        e.AddedItems.OfType<Person>()
            .ToList()
            .ForEach(ViewModel.SelectedPeople.Add);
    }

    if (e.RemovedItems != null)
    {
        e.RemovedItems.OfType<Person>()
            .ToList()
            .ForEach(p => ViewModel.SelectedPeople.Remove(p));
    }
}

为简洁起见,我删除了异常管理。

编辑 - 以上是关于处理来自DataGrid的多个选择。我已将上面的PersonModel重命名为Person。它是将数据公开给视图的模型。 ViewModel是管理用于操作模型的模型和功能的类。有关详细信息,请访问Google MVVM pattern

关于原始问题,处理DataGridComboBoxColumn和DataGrid之间的绑定,我创建了一个示例。随后的代码可以复制到新项目中。

Person模型是

public class Person
{
    public string FirstName { get; set; } 
    public string Surname { get; set; }
    public string MiddleName { get; set; }
    public int Age { get; set; }
}

ViewModel是

public class MyViewModel
{
    public MyViewModel()
    {
        this.People = new ObservableCollection<Person>()
        {
            new Person() { FirstName = "A", Surname = "B", MiddleName = "C", Age = 1},
            new Person() { FirstName = "D", Surname = "E", MiddleName = "F", Age = 2},
            new Person() { FirstName = "G", Surname = "H", MiddleName = "I", Age = 3},
            new Person() { FirstName = "J", Surname = "K", MiddleName = "L", Age = 4},
            new Person() { FirstName = "M", Surname = "N", MiddleName = "O", Age = 5}
        };

        this.Items = new ObservableCollection<GridItem>()
        {
            new GridItem() { Person = this.People[0]},
            new GridItem() { Person = this.People[1]},
            new GridItem() { Person = this.People[2]},
            new GridItem() { Person = this.People[3]}
        };
    }

    public ObservableCollection<Person> People { get; set; }

    public ObservableCollection<GridItem> Items { get; set; }

}

View背后的代码只是创建并公开ViewModel。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        ViewModel = new MyViewModel();
        InitializeComponent();
    }

    public MyViewModel ViewModel { get; set; }
}

在Xaml中留下所有乐趣。

<Window x:Class="StackOverflow._20902950.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:this="clr-namespace:StackOverflow._20902950"
        DataContext="{Binding RelativeSource={RelativeSource Self}, Path=ViewModel}"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="Person" Width="100" DisplayMemberPath="FirstName" SelectedItemBinding="{Binding Path=Person}">
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type this:MainWindow}}, Path=ViewModel.People}" />
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type this:MainWindow}}, Path=ViewModel.People}" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

此命名空间需要更改项目的命名空间。

我希望这会有所帮助。