Bind enum to telerik column in silverlight
I've found the example at telerik forum of binding enum to radgridview, but it doesn't work hwo 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 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?
<UserControl x:Class="BindingGridViewToEnumCollection.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
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>
My 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
{
开发者_Python百科 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));
}
}
}
namespace BindingGridViewToEnumCollection
{
/// <summary>
/// A football position.
/// </summary>
public enum Position
{
GK,
DF,
MF,
FW
}
}
EDIT
I'm using 2010.1.603.1040 version telerik, so I can't do i tlike there http://demos.telerik.com/silverlight/#GridView/EnumDataSource
Your DataMemberBinding
is wrong. You should add the mode:
DataMemberBinding="{Binding Position,Mode=TwoWay}"
<telerik:GridViewComboBoxColumn DataMemberBinding="{Binding Position,Mode=TwoWay}"
ItemsSource="{Binding Positions}">
<telerik:GridViewComboBoxColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Position}" />
</DataTemplate>
</telerik:GridViewComboBoxColumn.CellTemplate>
</telerik:GridViewComboBoxColumn>
One thing you can do is to add the following field and property to your MyViewModel
class:
private static readonly Position[] positions = new Position[]
{
Position.GK, Position.DF, Position.MF, Position.FW
};
public IEnumerable<Position> Positions
{
get { return positions; }
}
(There's no Enum.GetValues() in Silverlight, so we have to make our own collection of the enum's values.)
Then you can change the Position column of your RadGridView to the following:
<telerik:GridViewComboBoxColumn DataMemberBinding="{Binding Position}"
ItemsSource="{Binding Positions}">
<telerik:GridViewComboBoxColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Position}" />
</DataTemplate>
</telerik:GridViewComboBoxColumn.CellTemplate>
</telerik:GridViewComboBoxColumn>
All we really should need to get the combobox column working is to change the type of the column to GridViewComboBoxColumn
and add an ItemsSource
that specifies the items to show in the combobox. However, I found that the Position column had a habit of going blank when other columns were focused. Once I added the CellTemplate. I found that the Position column behaved itself a bit better.
Note that you will need to set the Mode
of your bindings to TwoWay
if you want the changes you make in the grid to be sent back to your view-model.
精彩评论