Binding enum to column

2 posts, 0 answers
  1. John
    John avatar
    32 posts
    Member since:
    Sep 2011

    Posted 03 Sep 2011 Link to this post

    I've found the example at this forum of binding enum to radgridview, but it doesn't work how I need, and I need it on today, so i need a help.

    Below are classes of this example, and only difference which I need is the possibility to change value of positionof players. Now I can only change position by typing new value, but I need to select it from comboboxcolumn.


    How can I do it?

    ViewModel

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    using System.Collections.Generic;
     
    namespace BindingGridViewToEnumCollection
    {
        public class MyViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
     
            private ObservableCollection<Club> clubs;
            private ObservableCollection<Player> players;
            private object selectedItem;       
             
     
            //public IEnumerable<string> AssignedPositions
            public IEnumerable<Position> AssignedPositions
            {
                get
                {
     
                    return new[] { Position.DF, Position.FW };
                     
                }
                 
            }
     
     
            public ObservableCollection<Club> Clubs
            {
                get
                {
                    if (this.clubs == null)
                    {
                        this.clubs = Club.GetClubs();
                    }
     
                    return this.clubs;
                }
            }
     
            public ObservableCollection<Player> Players
            {
                get
                {
                    if (this.players == null)
                    {
                        this.players = Player.GetPlayers();
                    }
     
                    return this.players;
                }
            }
     
            public object SelectedItem
            {
                get
                {
                    return this.selectedItem;
                }
                set
                {
                    if (value != this.selectedItem)
                    {
                        this.selectedItem = value;
                        this.OnPropertyChanged("SelectedItem");
                    }
                }
            }
     
            protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, args);
                }
            }
     
            private void OnPropertyChanged(string propertyName)
            {
                this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
            }
     
             
        }
    }
       

    Class Club:

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    using System.Collections.Generic;
    using System.Linq;
     
    namespace BindingGridViewToEnumCollection
    {
        /// <summary>
        /// A football club.
        /// </summary>
        public class Club : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
     
            private string name;
            private DateTime established;
            private int stadiumCapacity;
            private ObservableCollection<Player> players;
     
            public string Name
            {
                get { return this.name; }
                set
                {
                    if (value != this.name)
                    {
                        this.name = value;
                        this.OnPropertyChanged("Name");
                    }
                }
            }
     
            public DateTime Established
            {
                get { return this.established; }
                set
                {
                    if (value != this.established)
                    {
                        this.established = value;
                        this.OnPropertyChanged("Established");
                    }
                }
            }
     
            public int StadiumCapacity
            {
                get { return this.stadiumCapacity; }
                set
                {
                    if (value != this.stadiumCapacity)
                    {
                        this.stadiumCapacity = value;
                        this.OnPropertyChanged("StadiumCapacity");
                    }
                }
            }
     
            public ObservableCollection<Player> Players
            {
                get
                {
                    if (null == this.players)
                    {
                        this.players = new ObservableCollection<Player>();
                    }
     
                    return this.players;
                }
            }
     
            public Club()
            {
     
            }
     
            public Club(string name, DateTime established, int stadiumCapacity)
            {
                this.name = name;
                this.established = established;
                this.stadiumCapacity = stadiumCapacity;
            }
     
            public Club(string name, DateTime established, int stadiumCapacity, ObservableCollection<Player> players)
                : this(name, established, stadiumCapacity)
            {
                this.players = players;
            }
     
            protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, args);
                }
            }
     
            private void OnPropertyChanged(string propertyName)
            {
                this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
            }
     
            public override string ToString()
            {
                return this.Name;
            }
     
            public static ObservableCollection<Club> GetClubs()
            {
                ObservableCollection<Club> clubs = new ObservableCollection<Club>();
                Club club;
     
                // Liverpool
                club = new Club("Liverpool", new DateTime(1892, 1, 1), 45362);
                club.Players.Add(new Player("Pepe Reina", 25, Position.GK, "Spain"));
                club.Players.Add(new Player("Jamie Carragher", 23, Position.DF, "England"));
                club.Players.Add(new Player("Steven Gerrard", 8, Position.MF, "England"));
                club.Players.Add(new Player("Fernando Torres", 9, Position.FW, "Spain"));
                clubs.Add(club);
     
                // Manchester Utd.
                club = new Club("Manchester Utd.", new DateTime(1878, 1, 1), 76212);
                club.Players.Add(new Player("Edwin van der Sar", 1, Position.GK, "Netherlands"));
                club.Players.Add(new Player("Rio Ferdinand", 5, Position.DF, "England"));
                club.Players.Add(new Player("Ryan Giggs", 11, Position.MF, "Wales"));
                club.Players.Add(new Player("Wayne Rooney", 10, Position.FW, "England"));
                clubs.Add(club);
     
                // Chelsea
                club = new Club("Chelsea", new DateTime(1905, 1, 1), 42055);
                club.Players.Add(new Player("Petr Čech", 1, Position.GK, "Czech Republic"));
                club.Players.Add(new Player("John Terry", 26, Position.DF, "England"));
                club.Players.Add(new Player("Frank Lampard", 8, Position.MF, "England"));
                club.Players.Add(new Player("Nicolas Anelka", 39, Position.FW, "France"));
                clubs.Add(club);
     
                // Arsenal
                club = new Club("Arsenal", new DateTime(1886, 1, 1), 60355);
                club.Players.Add(new Player("Manuel Almunia", 1, Position.GK, "Spain"));
                club.Players.Add(new Player("Gaël Clichy", 22, Position.DF, "France"));
                club.Players.Add(new Player("Cesc Fàbregas", 4, Position.MF, "Spain"));
                club.Players.Add(new Player("Robin van Persie", 11, Position.FW, "Netherlands"));
                clubs.Add(club);
     
                return clubs;
            }
        }
    }

      

    class Player

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    using System.Linq;
     
    namespace BindingGridViewToEnumCollection
    {
        /// <summary>
        /// A football player.
        /// </summary>
        public class Player : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
     
            private string name;
            private int number;
            private Position position;
            private string country;
     
            public string Name
            {
                get { return this.name; }
                set
                {
                    if (value != this.name)
                    {
                        this.name = value;
                        this.OnPropertyChanged("Name");
                    }
                }
            }
     
            public int Number
            {
                get { return this.number; }
                set
                {
                    if (value != this.number)
                    {
                        this.number = value;
                        this.OnPropertyChanged("Number");
                    }
                }
            }
     
            public Position Position
            {
                get { return this.position; }
                set
                {
                    if (value != this.position)
                    {
                        this.position = value;
                        this.OnPropertyChanged("Position");
                    }
                }
            }
     
            public string Country
            {
                get { return this.country; }
                set
                {
                    if (value != this.country)
                    {
                        this.country = value;
                        this.OnPropertyChanged("Country");
                    }
                }
            }
     
            public Player()
            {
     
            }
     
            public Player(string name, int number, Position position, string country)
            {
                this.name = name;
                this.number = number;
                this.position = position;
                this.country = country;
            }
     
            protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, args);
                }
            }
     
            private void OnPropertyChanged(string propertyName)
            {
                this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
            }
     
            public override string ToString()
            {
                return this.Name;
            }
     
            public static ObservableCollection<Player> GetPlayers()
            {
                return new ObservableCollection<Player>(Club.GetClubs().SelectMany(c => c.Players));
            }
        }
    }


    Enum

    namespace BindingGridViewToEnumCollection
    {
        /// <summary>
        /// A football position.
        /// </summary>
        public enum Position
        {
            GK,
            DF,
            MF,
            FW
        }
    }



    and the xaml:

    <UserControl x:Class="BindingGridViewToEnumCollection.MainPage"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                 xmlns:my="clr-namespace:BindingGridViewToEnumCollection"
                 mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="700">
        <UserControl.Resources>
            <my:MyViewModel x:Key="MyViewModel"/>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot"
              Background="White"
              DataContext="{StaticResource MyViewModel}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <telerik:RadGridView Name="playersGrid" Grid.Row="0"
                                         ItemsSource="{Binding Players}"
                                         AutoGenerateColumns="False">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Number}"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Position}"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Country}"/>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
             
        </Grid>
    </UserControl>


    I'm using 2010.1.603.1040 version telerik, so I can't do i tlike therehttp://demos.telerik.com/silverlight/#GridView/EnumDataSource
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 05 Sep 2011 Link to this post

    Hi John,

    I would definitely recommend you to upgrade to our current official release Q2 2011. Thus you will benefit from all additional feature and bug fixed we have introduced.
    Otherwise, you will have to create a new source filled with the values equal to those in the enum and set it as ItemsSource for the GridViewComboBoxColumn.
     

    All the best,
    Maya
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Back to Top