如何在DataGridTextColumn SortMemberPath中实现转换器
本文关键字:实现 转换器 SortMemberPath DataGridTextColumn | 更新日期: 2023-09-27 18:26:46
我希望根据转换器返回的值对列进行排序。我有一个类,它包含两个属性VisitDate和VisitTime,但我在XAML中只有一列Visit Info来显示Visit Date和Time。因此,我创建了一个Converter来合并VisitDate和VisitTime,它按照我的喜好显示值,但我无法对列进行排序。
我的XAML源代码
<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>
模型和视图模型类源代码是
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;
}
}
为什么不在Employee对象中创建一个提供完整信息的Property VisitInfo?
public class Employee
{
public string empName { get; set; }
public string VisitDate { get; set; }
public string VisitTime { get; set; }
public DateTime VisitInfo
{
get { //Do your stuff here }
set { //Do your stuff here }
}
}
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" });
}
}
然后,您将在xaml:中绑定VisitInfo
<DataGrid Name="grdPendingList" ItemsSource="{Binding EmployeeList}">
<DataGridTextColumn Header="Employee" Binding="{Binding empName}" CanUserSort="True"/>
<DataGridTextColumn Header="Visit Info" Binding="{Binding VisitInfo}" CanUserSort="True"/>
</DataGrid>
它更简单,避免了您减少转换器的使用。希望这将帮助您
如果您希望按计算的数据排序,但没有更改数据结构的权限(或能力),您可以考虑以下方法之一:
- 使用将用于SortMemberPath的额外属性在应用程序上本地扩展类
- 处理DataGrid的";排序";事件,如果单击右列进行排序,则手动(通过自定义计算)对集合进行排序