2012-11-28 58 views
0

可以說,我有我加載到由在說明中指定的顏色開始我的數據網格數據網格的Silverlight的Datagrid組通過選擇值

ID | Desc 
------------------------ 
32  Red Cat 
33  Blue Dog 
34  Red Dog 
37  Green Zebra 
42  Reddish Snake 
47  Greenlike Gorilla 

我想我的組值以下的值。所以它會是這個樣子

ID | Desc 
---------------------------- 
Red: 
32  Red Cat 
34  Red Dog 
42  Reddish Snake 
Blue: 
33  Blue Dog 
Green: 
37  Green Zebra 
47  Greenlike Gorilla 

我有這個背後我的代碼:

PagedCollectionView pageView = new PagedCollectionView(IEnumerable<MyClass> main); 
pageView.GroupDescriptions.Add(new PropertyGroupDescription("")); //????? 
this.MyGrid.ItemsSource = pageView; 

我怎麼會指定分組參數?

回答

1

You can provide an IValueConverter給GroupDescription。因此,使用:

new PropertyGroupDescription("Color", new StringToColorConverter()) 

其中StringToColorConverter轉換說明屬性的顏色字符串:

public class StringToColorConverter: IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if ((string)value == null) return null; 
     return ((string)value).Split(new [] { ' ' }).FirstOrDefault();  
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

替代方法

如果你能修改該類,那麼你可以添加一個簡單的派生p roperty Color

class MyClass 
{ 
    public string Desc { get; set; } 
    public int ID { get; set; } 

    public string Color 
    { 
     get 
     { 
      return Desc.Split(new [] { ' ' }).FirstOrDefault();  
     } 
    } 
} 

然後你可以對它進行分組。

new PropertyGroupDescription("Color") 
+0

我創造自己的課程的另一種方法運作良好。謝謝。我嘗試了第一種方法,但不知道如何訪問我的數據源中的「Desc」列 – user1384831

0

PropertyGroupDescription只是GroupDescription的一個實現。你可以推出自己的。事實上,我們可以推出一個用於一般用途:

public class LambdaGroupDescription<T> : GroupDescription 
{ 
    public Func<T, object> GroupDelegate { get; set; } 

    public LambdaGroupDescription(Func<T, object> groupDelegate) 
    { 
     this.GroupDelegate = groupDelegate; 
    } 

    public override object GroupNameFromItem(object item, int level, System.Globalization.CultureInfo culture) 
    { 
     return this.GroupDelegate((T)item); 
    } 
} 

然後將其添加到PagedCollectionView:

var pageView = new PagedCollectionView(items); 
     pageView.GroupDescriptions.Add(new LambdaGroupDescription<ViewModel>(
      vm => vm.Description.Split(' ').FirstOrDefault()  
     )); 
     this.DataGrid.ItemsSource = pageView; 

編輯

看來您的分組邏輯是多一點點比簡單的分裂複雜。你可以嘗試這樣的:

public string FormatColor(string color) 
    { 
     if (string.IsNullOrWhiteSpace(color)) return null; 

     if (color.ToUpperInvariant().StartsWith("RED")) 
      return "Red"; 

     if (color.ToUpperInvariant().StartsWith("GREEN")) 
      return "Green"; 

     return color; 
    } 

然後:

pageView.GroupDescriptions.Add(new LambdaGroupDescription<ViewModel>(
      vm => FormatColor(vm.Description.Split(' ').FirstOrDefault() as string) 
     )); 

在FormatColor方法,你也可以用字典來「奇怪」的顏色值映射到已知的那些:

private static readonly Dictionary<string, string> ColorMap = new Dictionary<string, string>(){ 
     {"Greenlike", "Green"}, 
     {"Reddish", "Red"}, 
    }; 

public string FormatColor(string color) 
    { 
     if (string.IsNullOrWhiteSpace(color)) return null; 

     if (ColorMap.ContainsKey(color)) 
      return ColorMap[color]; 

     return color; 
    }