繁体   English   中英

Silverlight Datagrid按选择值分组

[英]Silverlight Datagrid Group by Select Values

可以说我有以下要加载到数据网格中的值

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;

如何指定分组参数?

您可以将IValueConverter提供给GroupDescription。 因此使用:

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

其中StringToColorConverterDesc属性转换为颜色字符串:

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

替代方法

如果您能够修改该类,则可以添加一个简单的派生属性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")

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方法中,您还可以使用Dictionary将“奇怪的”颜色值映射到已知的颜色值:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM