DataGrid - 如何使列排序动态,以满足绑定数据更改时的需求? [英] DataGrid - how to make Column sorting dynamic, to cater for when bound data changes?

查看:167
本文介绍了DataGrid - 如何使列排序动态,以满足绑定数据更改时的需求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS2010 WPF C#项目中使用了DataGrid。我把DataGrid绑定到了ObservableCollection。当您点击列标题时,会在该时间点对数据进行排序。



问题 - 我如何安排,以便DataGrid中的排序是动态的,因此数据更改(在ObservableCollection内)排序保持工作。



注意:绑定方法是通过DataGrid

  private ObservableCollection< SummaryItem> _summaryData = new ObservableCollection< SummaryItem>(); 
SummaryDataGrid.ItemsSource = _summaryData;

SummaryDataGrid.AutoGeneratingColumn + =(s,e)=>
{
// if(e.Column.Header.ToString()==ProcessName)
// e.Column.Width = new DataGridLength(1,DataGridLengthUnitType.Star);
e.Column.Width = new DataGridLength(1,DataGridLengthUnitType.Star);
};

public class SummaryItem:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

私有字符串_processName;
public string ProcessName
{
get {return _processName; }
set
{
_processName = value;
NotifyPropertyChanged(ProcessName);
}
}

private long _total;
public long总计
{
get {return _total; }
set
{
_total = value;
NotifyPropertyChanged(Total);
}
}

private long _average;
public long Average
{
get {return _average; }
set
{
_average = value;
NotifyPropertyChanged(Average);
}
}

private void NotifyPropertyChanged(string propertyName)
{
if(PropertyChanged!= null)
{
PropertyChanged(this,new PropertyChangedEventArgs((propertyName)));
}
}

public static SummaryItem ObservableCollectionSearch(ObservableCollection< SummaryItem> oc,string procName)
{
foreach(var summaryItem in oc)
{
if(summaryItem.ProcessName == procName)return summaryItem;
}
返回null;
}
}


解决方案

可以在后面的代码中使用CollectionViewSource,而在XAML中,它的源代码是你的datagrid的itemsource,那么你可以添加SortDescription / s。这将保持数据一直排序。


I'm using a DataGrid in a VS2010 WPF C# project. I have bound the DataGrid to an ObservableCollection. When you click on a column heading it sorts the data at that point in time.

Question - How would I arrange such that the sorting in the DataGrid is dynamic, so that when data changes (within the ObservableCollection) the sorting keeps working.

Notes: Binding approach is via DataGrid

private ObservableCollection<SummaryItem> _summaryData = new ObservableCollection<SummaryItem>();
SummaryDataGrid.ItemsSource = _summaryData;

SummaryDataGrid.AutoGeneratingColumn += (s, e) =>
{
    //if (e.Column.Header.ToString() == "ProcessName")
    //    e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
    e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
};

public class SummaryItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _processName;
    public string ProcessName
    {
        get { return _processName; }
        set
        {
            _processName = value;
            NotifyPropertyChanged("ProcessName");
        }
    }

    private long _total;
    public long Total
    {
        get { return _total; }
        set
        {
            _total = value;
            NotifyPropertyChanged("Total");
        }
    }

    private long _average;
    public long Average
    {
        get { return _average; }
        set
        {
            _average = value;
            NotifyPropertyChanged("Average");
        }
    }

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs((propertyName)));
        }
    }

    public static SummaryItem ObservableCollectionSearch(ObservableCollection<SummaryItem> oc, string procName)
    {
        foreach (var summaryItem in oc)
        {
            if (summaryItem.ProcessName == procName) return summaryItem;
        }
        return null;
    }
}

解决方案

You can use CollectionViewSource in code behind as well as in XAML, whose source is your datagrid's itemsource, then you can add the SortDescription/s to it. This will keep the data sorted all the time.

这篇关于DataGrid - 如何使列排序动态,以满足绑定数据更改时的需求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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