Xceed DataGrid显示组合框 [英] Xceed datagrid showing combobox

查看:81
本文介绍了Xceed DataGrid显示组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Xceed DataGrid for WPF社区版来显示WPF应用程序中已经对Propertygrid正确的项目。

I wanted to use "Xceed DataGrid for WPF" community edition to show items in WPF application that are already coming correct for a Propertygrid.

文本框(名字)可以正常工作,但是组合框不能工作。问题是combobox没有填充任何内容,并且没有正确设置性别。我的简单代码如下。

The textbox (first name) is working fine, but combobox is not working. Problem is combobox is not populating anything and it is not setting the gender correctly. My simple code is given below.

XAML:

    <Window.Resources>
            <xcdg:DataGridCollectionViewSource x:Key="mySource" Source="{Binding Path=SelectedEntity}" />
</Window.Resources>
<Grid>
    <xcdg:DataGridControl ItemsSource="{Binding Source={StaticResource mySource}}" AutoCreateColumns="True">
        <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="FirstName" Title="First Name" />
            <xcdg:Column  FieldName="Gender" Title="Gender" >
                <xcdg:Column.CellContentTemplate>
                    <DataTemplate x:Name="clmGenderTmp">
                        <ComboBox SelectedValuePath="GenderID"
                            DisplayMemberPath="Name"
                            ItemsSource="{Binding Path=SelectedEntity.Gender, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                            SelectedValue="{xcdg:CellEditorBinding}"/>
                    </DataTemplate>
                </xcdg:Column.CellContentTemplate>
            </xcdg:Column>
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
</Grid>

Xaml.cs是:

InitializeComponent();
this.DataContext = new MainWindowViewModel();

数据类为:

using System.ComponentModel;
using Xceed.Wpf.DataGrid;  
public enum Genders { Male, Female  }
public class Person
{
    public Person(string firstName, Genders gender)
    {
        FirstName = firstName;
        Gender = gender;
    }

    [DisplayName("Given name")]
    public string FirstName { get; set; }

    [Browsable(true)]
    public Genders Gender {  get;  set; }
}

查看模型为:

public class MainWindowViewModel
{
    public List<object> SelectedEntity { get; set; }
    public MainWindowViewModel()
    {
        SelectedEntity = new List<object>();                    
        this.SelectedEntity.Add(new Person("Mathew", Genders.Male));
        this.SelectedEntity.Add(new Person("Mark", Genders.Female));
    }
}

推荐答案

似乎 Window 没有属性 SelectedEntity 。 ;-)您要绑定到的是 DataContext.SelectedEntity.Gender 。无论如何,这根本不起作用,因为您试图将单个值(性别)绑定为 ItemsSource

It seems that Window does not have a property SelectedEntity. ;-) What you wanted to bind to is DataContext.SelectedEntity.Gender. Anyway, this will not work at all, because you are trying to bind a single value (Gender) as a ItemsSource.

您需要向组合框提供(枚举)值的列表。
如何将枚举绑定到WPF中的组合框控件?

You need to provide a list of (enum) values to the combobox. How to bind an enum to a combobox control in WPF?

编辑

作为我的答案显然不够具体,我举一个完整的例子:

As my answer was obviously not concrete enough, I provide a full example:

XAML:

<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Firstname}"
                            Margin="10" />
                <ComboBox ItemsSource="{Binding Path=AvailableGenders}"
                            SelectedValue="{Binding Path=Gender}"
                            Grid.Column="1" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

PersonViewModel:

PersonViewModel:

public class PersonViewModel : ViewModelBase
{
    private Genders _gender;
    private string _firstname;
    public string Firstname
    {
        get
        {
            return _firstname;
        }
        set
        {
            _firstname = value;
            OnPropertyChanged();
        }
    }

    public Genders Gender
    {
        get
        {
            return _gender;
        }
        set
        {
            _gender = value;
            OnPropertyChanged();
        }
    }

    public List<Genders> AvailableGenders

    {
        get
        {
            return Enum.GetValues(typeof(Genders)).Cast<Genders>().ToList();
        }
    }

}

MainWindow Ctor :

MainWindow Ctor:

public MainWindow()
    {
    InitializeComponent();

    List<PersonViewModel> persons = new List<PersonViewModel>();


    persons.Add(new PersonViewModel() { Firstname = "John", Gender = Genders.female });
    persons.Add(new PersonViewModel() { Firstname = "Partyboy", Gender = Genders.male });
    persons.Add(new PersonViewModel() { Firstname = "r2d2", Gender = Genders.robot });
    persons.Add(new PersonViewModel() { Firstname = "KlausMaria", Gender = Genders.shemale });

    DataContext = persons;


}

这篇关于Xceed DataGrid显示组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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