0

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));
    }
}

The result I am getting is as follows. As you can see I get the first column correctly but my combobox does not have the value and combobox items are not appearing.:[![enter image description here

2 Answers2

1

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;


}
Community
  • 1
  • 1
Mat
  • 1,960
  • 5
  • 25
  • 38
  • It gets even more confusing when you name your list of provided Persons SelectedObject. I would suggest you to use better names. – Mat Nov 02 '16 at 09:55
  • I have modifed my question with the result window. Please observe I get first name correctly, only the combobox fails. – OrionSoftTechnologiesSydney Nov 02 '16 at 11:40
  • use a textbox and you will see the gender. The problem is that you have to provide the items that should be shown in the combobox (see the link I've provided). Therefore you have to use the ItemsSource Property. – Mat Nov 02 '16 at 12:31
  • Mat, your example working fine, but I am specifically looking for Xceed datagrid however your the example is really useful. – OrionSoftTechnologiesSydney Nov 03 '16 at 00:40
  • your problem is not the datagrid. You have a problem with your combobox. Please consider also [mcve]. The Xceed datagrid is actually inherited from ItemsControl https://xceed.com/wp-content/documentation/xceed-toolkit-plus-for-wpf/Xceed.Wpf.DataGrid~Xceed.Wpf.DataGrid.DataGridControl.html – Mat Nov 03 '16 at 08:44
0

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>
Community
  • 1
  • 1