DataGrid - 如何使列排序动态化,以满足绑定数据更改的需要?

发布于 2024-09-26 14:06:18 字数 1895 浏览 1 评论 0原文

我在 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;

    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;
    }
}

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友欢 2024-10-03 14:06:18

您可以在代码隐藏以及 XAML 中使用 CollectionViewSource,其源是数据网格的 itemsource,然后您可以向其中添加 SortDescription。这将使数据始终保持排序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文