2011-09-03 35 views
0

我在telerik論壇上發現了綁定enum到radgridview的示例,但它不起作用,我需要它,今天我需要它,所以我需要幫助。將enum綁定到silverlight中的telerik列表

下面是這個例子的類,只有我需要的差異是可以改變玩家位置的價值。現在我只能通過輸入新值來改變位置,但我需要從combobox列中選擇它。

我該怎麼辦?

<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> 

我的視圖模型

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)); 
     } 


    } 
} 

類俱樂部:

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; 
     } 
    } 
} 

類球員

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的,所以我不能做我tlike有 http://demos.telerik.com/silverlight/#GridView/EnumDataSource

回答

0

一兩件事你可以做的是以下字段和屬性添加到您的MyViewModel類:

private static readonly Position[] positions = new Position[] 
    { 
     Position.GK, Position.DF, Position.MF, Position.FW 
    }; 

    public IEnumerable<Position> Positions 
    { 
     get { return positions; } 
    } 

(有在Silverlight沒有Enum.GetValues(),所以我們必須使我們自己的枚舉的值的集合。)

然後您可以將RadGridView的位置欄更改爲以下:

  <telerik:GridViewComboBoxColumn DataMemberBinding="{Binding Position}" 
              ItemsSource="{Binding Positions}"> 
       <telerik:GridViewComboBoxColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Position}" /> 
        </DataTemplate> 
       </telerik:GridViewComboBoxColumn.CellTemplate> 
      </telerik:GridViewComboBoxColumn> 

我們真正需要獲得組合框的工作方式是將列的類型更改爲GridViewComboBoxColumn並添加一個ItemsSource,該組合框指定要在組合框中顯示的項目。然而,我發現當其他專欄專注時,位置欄有一個空白的習慣。一旦我添加了CellTemplate。我發現Position列的表現更好一些。

請注意,如果您希望將網格中所做的更改發送回您的視圖模型,您需要將綁定的Mode設置爲TwoWay

+0

可惜每次我嘗試選擇組合框的值此列失去價值,而組合框爲空。但謝謝你的嘗試 – user278618

1

您的DataMemberBinding是錯誤的。您應該添加方式:

DataMemberBinding =「{結合位置,模式=雙向}」

<telerik:GridViewComboBoxColumn DataMemberBinding="{Binding Position,Mode=TwoWay}" 
             ItemsSource="{Binding Positions}"> 
    <telerik:GridViewComboBoxColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Position}" /> 
     </DataTemplate> 
    </telerik:GridViewComboBoxColumn.CellTemplate> 
</telerik:GridViewComboBoxColumn> 
相關問題