对DateGridTemplateColumn MultiBinding进行排序

时间:2015-10-20 13:56:47

标签: c# wpf datagrid

在我的应用程序的视图中,我对InsertionSort insertionSort; BubbleSort bubbleSort; void setup() { String url = sketchPath("numbers/512/"); insertion = new Insertion(); String[] stringData = null; int[] intData = null; runTest(stringData, intData, url, insertionSort); runTest(stringData, intData, url, bubbleSort); } void runTest(String[] text, int[] number, String url, SortingAlgorithm algorithm) { File directory = new File(url); File[] listOfFiles = directory.listFiles(); for (File file : listOfFiles) { //println(file.getName()); text = loadStrings(file); number = int(text); algorithm.sort(number); } }

有以下定义
DataGridTemplateColumn

MultiDateTimeConverter是:

<DataGridTemplateColumn Header="Date and Time" Width="Auto" SortMemberPath="ModuleInfos.FileCreationDateTime">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Width="16" Height="16" ToolTip="Original date changed!"
                       Source="pack://application:,,,/UI.Resources;component/Graphics/InformationImage.png">
                    <Image.Visibility>
                        <MultiBinding Converter="{converters:MultiDateTimeConverter}">
                            <Binding Path="ModuleInfos.FileCreationDateTime" UpdateSourceTrigger="PropertyChanged"/>
                            <Binding Path="PartListInfos.ModuleDateTime" UpdateSourceTrigger="PropertyChanged"/>
                        </MultiBinding>
                    </Image.Visibility>
                </Image>
                <TextBlock Grid.Column="1" Style="{StaticResource DataGridTextBlockStyle}">
                    <TextBlock.Text>
                        <MultiBinding Converter="{converters:MultiDateTimeConverter}">
                            <Binding Path="ModuleInfos.FileCreationDateTime" UpdateSourceTrigger="PropertyChanged"/>
                            <Binding Path="PartListInfos.ModuleDateTime" UpdateSourceTrigger="PropertyChanged"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </Grid>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我的问题是internal class MultiDateTimeConverter : MarkupExtension, IMultiValueConverter { private static MultiDateTimeConverter converter; private const string dateFormatString = "dd.MM.yyyy - HH:mm:ss"; public MultiDateTimeConverter() { } public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values != null && values.Length == 2 && values[0] is DateTime) { if (values[1] is DateTime) { if(targetType == typeof(string)) return ((DateTime) values[1]).ToString(dateFormatString); if (targetType == typeof (Visibility)) return Visibility.Visible; } if(targetType == typeof(string)) return ((DateTime)values[0]).ToString(dateFormatString); if (targetType == typeof (Visibility)) return Visibility.Collapsed; } return values; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } public override object ProvideValue(IServiceProvider serviceProvider) { return converter ?? (converter = new MultiDateTimeConverter()); } } 的{​​{1}}。在过去,列的内容刚刚绑定到属性SortMemberPath。但现在内容取决于两个属性DataGridTemplateColumnModuleInfos.FileCreationDateTime

如何才能将排序设置为正确的值?

1 个答案:

答案 0 :(得分:0)

为DataGrid的ItemsSource添加任意数量的属性的排序描述。如果单击FileName列标题,下面的代码将按两个属性排序。使用e.Handled = true来停止默认排序行为。

概念上这样做:

private void Dgrd_Sorting(object sender, DataGridSortingEventArgs e)
        {            
            DataGridColumn col = e.Column;
            if (col.Header.ToString() == "FileName")
            {
                CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(Dgrd.ItemsSource);
                view.SortDescriptions.Add(new System.ComponentModel.SortDescription("FileName", System.ComponentModel.ListSortDirection.Ascending));
                view.SortDescriptions.Add(new System.ComponentModel.SortDescription("CreatedBy", System.ComponentModel.ListSortDirection.Ascending));
                view.Refresh();

                e.Handled = true;
            }
            else
                e.Handled = true;
        }