将枚举绑定到 silverlight 中的 Telerik 列
我在 telerik 论坛上找到了将枚举绑定到 radgridview 的示例,但它无法按我需要的方式工作,而且我今天需要它,所以我需要帮助。
下面是这个例子的类,我需要的唯一区别是改变玩家位置值的可能性。现在我只能通过输入新值来更改位置,但我需要从组合框列中选择它。
我该怎么做呢?
<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>
我的 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));
}
}
}
namespace BindingGridViewToEnumCollection
{
/// <summary>
/// A football position.
/// </summary>
public enum Position
{
GK,
DF,
MF,
FW
}
}
编辑
我正在使用 2010.1.603.1040 版本 telerik,所以我不能在那里做 http://demos.telerik.com/silverlight/#GridView/EnumDataSource
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
{
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
DataMemberBinding
是错误的。您应该添加模式:DataMemberBinding="{Binding Position,Mode=TwoWay}"
Your
DataMemberBinding
is wrong. You should add the mode:DataMemberBinding="{Binding Position,Mode=TwoWay}"
您可以做的一件事是将以下字段和属性添加到您的
MyViewModel
类中:(没有 Enum.GetValues() 在 Silverlight 中,因此我们必须创建自己的枚举值集合。)
然后您可以更改 RadGridView 的 Position 列如下所示:
要使组合框列正常工作,我们真正需要的是将列的类型更改为 GridViewComboBoxColumn 并添加一个指定要显示的项目的
ItemsSource
组合框。然而,我发现当其他列获得焦点时,“位置”列有一个习惯性的空白。一旦我添加了 CellTemplate。我发现“位置”列本身表现得更好一些。请注意,如果您希望将网格中所做的更改发送回视图模型,则需要将绑定的
Mode
设置为TwoWay
。One thing you can do is to add the following field and property to your
MyViewModel
class:(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:
All we really should need to get the combobox column working is to change the type of the column to
GridViewComboBoxColumn
and add anItemsSource
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 toTwoWay
if you want the changes you make in the grid to be sent back to your view-model.