This project has moved. For the latest updates, please go here.

Datagrid with Sublist

Apr 27, 2015 at 12:07 PM
Hi all!
I am testing the extended wpf toolkit right now. What I need to achieve is to display entries in the datagrid, which have a enumerable property.

I am already able to display my entries - was very easy by the way - thank you for this great control.

But now I need to display another column, which contains an enumeration of values for each entry.

Can this be achieved with this datagrid - if so - how?

Thank you all for your help.

Cheers.
Developer
Apr 28, 2015 at 2:37 PM
Hi,

A column needs to have a type. If the enumerable property has the same type for each entries, it should work :
<Window x:Class="WpfApplication46.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
        xmlns:local="clr-namespace:WpfApplication46"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
   <Window.Resources>
      <ObjectDataProvider x:Key="TheEnumData"
                          MethodName="GetValues"
                          ObjectType="{x:Type sys:Enum}">
         <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:TheEnum" />
         </ObjectDataProvider.MethodParameters>
      </ObjectDataProvider>
      
      <xcdg:CellEditor x:Key="enumEditor">
         <xcdg:CellEditor.EditTemplate>
            <DataTemplate>
               <ComboBox BorderThickness="0"
                         MinHeight="22"
                         VerticalContentAlignment="Top"
                         ItemsSource="{Binding Source={StaticResource TheEnumData}}"
                         SelectedValue="{xcdg:CellEditorBinding}"
                         FocusVisualStyle="{x:Null}">
               </ComboBox>
            </DataTemplate>
         </xcdg:CellEditor.EditTemplate>
      </xcdg:CellEditor>
   </Window.Resources>
    
   <Grid>
      <xcdg:DataGridControl x:Name="_dataGrid">
         <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="MyName" />
            <xcdg:Column FieldName="MyEnum"
                         CellEditor="{StaticResource enumEditor}"/>
         </xcdg:DataGridControl.Columns>
      </xcdg:DataGridControl>
   </Grid>
</Window>
namespace WpfApplication46
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      _dataGrid.ItemsSource = new List<MyData>()
      {
        new MyData() { MyName = "First", MyEnum = TheEnum.Blue },
        new MyData() { MyName = "Second", MyEnum = TheEnum.Blue },
        new MyData() { MyName = "Third", MyEnum = TheEnum.Green },
        new MyData() { MyName = "Fourth", MyEnum = TheEnum.Red },
        new MyData() { MyName = "Fifth", MyEnum = TheEnum.Red },
      };
    }
  }

  public enum TheEnum
  {
    Red, 
    Green, 
    Blue
  }

  public class MyData
  {
    public string MyName
    {
      get;
      set;
    }

    public TheEnum MyEnum
    {
      get;
      set;
    }
  }
}