This project is read-only.

Class name in group header when grouping with a DataGridTemplateColumn

Jul 3, 2014 at 8:26 PM
Hello,

I really appreciate the extended data grid but I cannot display the correct group header when I group my data using a column defined with "DataGridTemplateColumn". Instead of the property value I got the item class name.

I've update the TestClient.UserControls.ColumnChooserPlacerControl class found int the example project as follow:

in the xaml part,
  • in the resource
<userControls:StatusConverter x:Key="StatusConverter"/>
  • between the owner column and the publisher column)
                <Controls:DataGridTemplateColumn Header="Status">
                    <Controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Status, Converter={StaticResource StatusConverter}}" />
                        </DataTemplate>
                    </Controls:DataGridTemplateColumn.CellTemplate>
                    <Controls:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding DataContext.StatusList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                      DisplayMemberPath="StatusString" SelectedValuePath="StatusEnum" SelectedValue="{Binding Status}"
                                      />
                        </DataTemplate>
                    </Controls:DataGridTemplateColumn.CellEditingTemplate>
                </Controls:DataGridTemplateColumn>
In the code behind part
    /// <summary>
    /// Interaction logic for PlacerControl.xaml
    /// </summary>
    public partial class ColumnChooserPlacerControl : INotifyPropertyChanged
    {
        private ObservableCollection<Game> _games;
        private FilterableNotifiableCollection<Game> _filterableNotifiableGamesCollection;
        private ICollectionView _collectionView;

        public ColumnChooserPlacerControl()
        {

            DataContext = this;
            InitializeComponent();

            Games = new ObservableCollection<Game>();
            Games.Add(new Game() { Creator = "Blizzard", Publisher = "Blizzard", GameName = "WorldOf Warcraft", Owner = "Mark", Status = StatusEnum.Insite});
            Games.Add(new Game() { Creator = "Bungie", Publisher = "Microsoft", GameName = "Halo", Owner = "Bill", Status = StatusEnum.Insite });
            Games.Add(new Game() { Creator = "Epic", Publisher = "Microsoft", GameName = "Gear Of War", Owner = "Steve", Status = StatusEnum.Loan });
            
            
            CollectionView = CollectionViewSource.GetDefaultView(Games);            

            StatusList = new List<StatusComboboxItem>
            {
                new StatusComboboxItem(StatusEnum.Insite,"in site"),
                new StatusComboboxItem(StatusEnum.Loan, "loan"),
                new StatusComboboxItem(StatusEnum.Lost,"definitively lost"),
            };

        }

        public List<StatusComboboxItem> StatusList { get; set; }
        public ObservableCollection<Game> Games
        {
            get { return _games; }
            set { _games = value; NotifyPropertyChanged("Games"); }
        }


        public ICollectionView CollectionView
        {
            get { return _collectionView; }
            set
            {
                _collectionView = value;
                NotifyPropertyChanged("CollectionView");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }

    public class StatusComboboxItem
    {
        public StatusEnum StatusEnum { get; set; }
        public string StatusString { get; set; }

        public StatusComboboxItem(StatusEnum status, string value)
        {
            StatusEnum = status;
            StatusString = value;
        }
    }

    public class Game : INotifyPropertyChanged, IEditableObject
    {
        private string _creator;
        private string _gameName;
        private string _owner;
        private string _publisher;


        public string Creator
        {
            get { return _creator; }
            set
            {
                if (value == _creator) return;
                _creator = value;
                OnPropertyChanged("Creator");
            }
        }

        public string GameName
        {
            get { return _gameName; }
            set
            {
                if (value == _gameName) return;
                _gameName = value;
                OnPropertyChanged("GameName");
            }
        }

        public string Owner
        {
            get { return _owner; }
            set
            {
                if (value == _owner) return;
                _owner = value;
                OnPropertyChanged("Owner");
            }
        }

        public string Publisher
        {
            get { return _publisher; }
            set
            {
                if (value == _publisher) return;
                _publisher = value;
                OnPropertyChanged("Publisher");
            }
        }

        public StatusEnum Status
        {
            get { return _status; }
            set
            {
                if (value == _status) return;
                _status = value;
                OnPropertyChanged("Status");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        private Game copy;
        private StatusEnum _status;

        public void BeginEdit()
        {
            if (this.copy == null)
                this.copy = new Game();
            copy.Creator = this.Creator;
            copy.GameName = this.GameName;
            copy.Publisher = this.Publisher;
            copy.Owner = this.Owner;
            copy.Status = this.Status;

        }

        public void EndEdit()
        {
            copy.Creator = null;
            copy.GameName = null;
            copy.Publisher = null;
            copy.Owner = null;
            
        }

        public void CancelEdit()
        {
            this.Creator = copy.Creator;
            this.GameName = copy.GameName;
            this.Publisher = copy.Publisher;
            this.Owner = copy.Owner;
            this.Status = copy.Status;

        }
    }

    public enum StatusEnum
    {
        Loan,
        Lost,
        Insite
    }


    public class StatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.GetType() != typeof (StatusEnum)) return value;
            switch ((StatusEnum)value)
            {
                    case StatusEnum.Insite:
                    return "in site";
                    case StatusEnum.Loan:
                    return "loan";
                case StatusEnum.Lost:
                    return "definitively lost";
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
I launch the Test app and choose the Column Chooser sample in the left.
When I drag the Status Column Header to the Group zone in the top, instead of having each group title defined with the different value of Status, I got the row datacontext object class name (TestClient.UserControls.Game) and the row ares not grouped (one group per row even for the rows that shared the same status value)

How can I correctly group the row and display the right title ?

Thank you.
Jul 17, 2014 at 10:35 AM
Edited Jul 17, 2014 at 10:36 AM
HI,

It will Show but you can change the Foreground color to transparent. Will that do you .If yes then do let me know I will let you know where and what change is required

Regards