我想使用“Xceed DataGrid for WPF”社区版来显示WPF应用程序中已经正确用于Propertygrid的项目。
文本框(名字)工作正常,但组合框不起作用。问题是组合框没有填充任何东西而且它没有正确设置性别。我的简单代码如下所示。
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));
}
}
答案 0 :(得分:1)
似乎Window
没有属性SelectedEntity
。 ;-)你想要绑定的是DataContext.SelectedEntity.Gender
。无论如何,这根本不起作用,因为你试图将单个值(Gender)绑定为ItemsSource
。
您需要为组合框提供(枚举)值列表。 How to bind an enum to a combobox control in WPF?
修改强>
由于我的答案显然不够具体,我提供了一个完整的例子:
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:
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:
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;
}
答案 1 :(得分:0)
我从How to add ComboBox column in XCeed DataGridControl (WPF)
得到了一个类似的例子如果有人遇到同样的问题,只需发布我的代码。
ViewModel:
public class ViewModel
{
public DataTable Persons { get; set; }
public DataTable Gender { get; set; }
public ViewModel()
{
Persons = new DataTable();
Persons.Columns.Add("FirstName", typeof(string));
Persons.Columns.Add("GenderID", typeof(int));
Persons.Rows.Add("Mathew",1);
Persons.Rows.Add("Gil", 2);
Gender = new DataTable();
Gender.Columns.Add("Name", typeof(string));
Gender.Columns.Add("GenderID", typeof(int));
Gender.Rows.Add("Male", 1);
Gender.Rows.Add("Female", 2);
}
}
代码背后:
InitializeComponent();
viewModel = new ViewModel();
this.DataContext = viewModel;
<强> XMAL:强>
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type xcdg:GroupByControl}">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</Grid.Resources>
<xcdg:DataGridControl ItemsSource="{Binding Persons}" >
<xcdg:DataGridControl.Columns>
<xcdg:Column x:Name="clmFN" FieldName="FirstName"/>
<xcdg:Column x:Name="clmGender" FieldName="GenderID">
<xcdg:Column.CellContentTemplate>
<DataTemplate x:Name="clmGenderTmp">
<ComboBox SelectedValuePath="GenderID"
DisplayMemberPath="Name"
ItemsSource="{Binding Path=DataContext.Gender, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
SelectedValue="{xcdg:CellEditorBinding}"/>
</DataTemplate>
</xcdg:Column.CellContentTemplate>
</xcdg:Column>
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>