如何在 DataGridTextColumn SortMemberPath 中实现转换器? [英] How to Implement Converter in DataGridTextColumn SortMemberPath?

查看:20
本文介绍了如何在 DataGridTextColumn SortMemberPath 中实现转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望根据转换器返回的值对列进行排序.我有一个类,它包含两个属性 VisitDate 和 VisitTime,但我在 XAML 中只有一列用于显示访问日期和时间的访问信息.因此,我创建了一个用于合并 VisitDate 和 VisitTime 的转换器,它显示我喜欢的值,但我无法对列进行排序.

I wish to Sort the Column based on the value returning by the Converter. I'm having a class it contains two property VisitDate and VisitTime, but I'm having only one column for Visit Info in the XAML to display Visit Date and Time. So, I created one Converter for merging VisitDate and VisitTime, it displays the value as I preferred, but I can't able to sort the column.

我的 XAML 源代码

My XAML Source Code

<DataGrid Name="grdPendingList" ItemsSource="{Binding EmployeeList}">
    <DataGrid.Resources>
        <ui:DateTimeMergingConverter x:Key="DateTimeMergingConverterKey"></ui:DateTimeMergingConverter>
    </DataGrid.Resources>
    <DataGridTextColumn Header="Employee" Binding="{Binding empName}" CanUserSort="True"/>
    <DataGridTextColumn Header="Visit Info" Binding="{Binding Converter={StaticResource DateTimeMergingConverterKey}}" CanUserSort="True" SortMemberPath="{Binding Converter={StaticResource DateTimeMergingConverterKey}}"/>
</DataGrid>

模型和视图模型类源代码是

The Model and View Model Class Source Code is

public class Employee
{
    public string empName { get; set; }
    public string VisitDate { get; set; }
    public string VisitTime { get; set; }
}

public class EmployeeInfo
{
    ObservableCollection<Employee> EmployeeList = new ObservableCollection<Employee>();
    public EmployeeInfo()
    {
        EmployeeList.Add(new Employee { empName = "John", VisitDate = "11/28/2015", VisitTime = "05:12 PM" });
        EmployeeList.Add(new Employee { empName = "Potter", VisitDate = "10/28/2015", VisitTime = "04:33 PM" });
        EmployeeList.Add(new Employee { empName = "James", VisitDate = "11/27/2015", VisitTime = "09:12 AM" });
    }
}

转换器源代码是

public class DateTimeMergingConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            if (value != null)
            {
                Employee pData = value as Employee;
                string dateStr = (string.IsNullOrEmpty(pData.VisitDate) ? string.Empty : ((pData.VisitDate).Split('/')).Count() > 2 ? pData.VisitDate : pData.VisitDate + "/2015").ToString() + " " + (string.IsNullOrEmpty(pData.VisitTime) ? string.Empty : (pData.VisitTime)).ToString();
                DateTime dt = string.IsNullOrEmpty(dateStr.Trim()) ? DateTime.Now : DateTime.Parse(dateStr);
                return dt;
            }
            else
            {
                return string.Empty;
            }
        }
        catch (Exception)
        {
            return string.Empty;
        }
    }

    public object ConvertBack(object o, Type type, object parameter, CultureInfo culture)
    {
        return null;
    }
}

推荐答案

如果您希望按计算数据排序但没有访问权(或能力)来更改数据的结构,您可以考虑以下方法之一:

If you wish to sort by calculated data but do not have an access (or ability) to change the structure of the data you may consider one of the following methods:

  1. 使用将用于 SortMemberPath 的额外属性在您的应用程序本地扩展类
  2. 处理 DataGrid 的排序"如果单击右侧的列进行排序,则手动(通过自定义计算)对您的集合进行排序的事件.

这篇关于如何在 DataGridTextColumn SortMemberPath 中实现转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆