Reputation: 650
I wanted to use "Xceed DataGrid for WPF" community edition to show items in WPF application that are already coming correct for a Propertygrid.
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 is:
InitializeComponent();
this.DataContext = new MainWindowViewModel();
Data class is:
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; }
}
View Model is:
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));
}
}
Upvotes: 0
Views: 1058
Reputation: 650
I got a similar example from How to add ComboBox column in XCeed DataGridControl (WPF)
Just publishing my code, if anyone faces same issue.
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);
}
}
Code Behind:
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>
Upvotes: 0
Reputation: 2072
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
.
You need to provide a list of (enum) values to the combobox. How to bind an enum to a combobox control in WPF?
EDIT
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:
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;
}
Upvotes: 1