我希望根据Converter返回的值对列进行排序。我有一个类,它包含两个属性VisitDate和VisitTime,但我在XAML中只有一个用于访问信息的列来显示访问日期和时间。因此,我创建了一个用于合并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;
}
}
答案 0 :(得分:0)
为什么你不只是在你的员工对象中创建一个提供完整信息的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>
它更简单,避免您减少转换器的使用。希望这会对你有所帮助
答案 1 :(得分:0)
如果您希望按计算数据排序但没有访问权(或能力)来更改数据的结构,您可以考虑以下方法之一: