wpf 纯粹在 XAML 中对组合框进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2274690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:30:19  来源:igfitidea点击:

Sorting a combobox purely in XAML

wpfxamlsortingcombobox

提问by Dave

I'm surprised that no one has asked this before here... well, at least I haven't found an answer here or anywhere else, actually.

我很惊讶之前没有人在这里问过这个问题……好吧,实际上,至少我在这里或其他任何地方都没有找到答案。

I have a ComboBox that is databound to an ObservableCollection. Everything worked great until the guys wanted the contents sorted. No problem -- I end up changing the simple property out:

我有一个数据绑定到 ObservableCollection 的 ComboBox。一切都很好,直到这些人想要对内容进行排序。没问题——我最终改变了简单的属性:

public ObservableCollection<string> CandyNames { get; set; } // instantiated in constructor

for something like this:

对于这样的事情:

private ObservableCollection<string> _candy_names; // instantiated in constructor
public ObservableCollection<string> CandyNames
{
    get {
        _candy_names = new ObservableCollection<string>(_candy_names.OrderBy( i => i));
        return _candy_names;
    }
    set {
        _candy_names = value;
    }
}

This post is really two questions in one:

这篇文章实际上是两个问题合二为一:

  1. How can I sort a simple ComboBox of strings in XAML only. I have researched this and can only find info about a SortDescription class, and this is the closest implementation I could find, but it wasn't for a ComboBox.
  2. Once I implemented the sorting in code-behind, it my databinding was broken; when I added new items to the ObservableCollection, the ComboBox items didn't update! I don't see how that happened, because I didn't assign a name to my ComboBox and manipulate it directly, which is what typically breaks the binding.
  1. 如何在 XAML 中对简单的字符串 ComboBox 进行排序。我对此进行了研究,只能找到有关 SortDescription 类的信息,这是我能找到的最接近的实现,但它不适用于 ComboBox。
  2. 一旦我在代码隐藏中实现了排序,我的数据绑定就被破坏了;当我向 ObservableCollection 添加新项目时,ComboBox 项目没有更新!我不明白这是怎么发生的,因为我没有为我的 ComboBox 分配名称并直接操作它,这通常会破坏绑定。

Thanks for your help!

谢谢你的帮助!

采纳答案by Wallstreet Programmer

You can use a CollectionViewSource to do the sorting in XAML, however you need to refresh it's view if the underlying collection changes.

您可以使用 CollectionViewSource 在 XAML 中进行排序,但是如果基础集合发生更改,您需要刷新它的视图。

XAML:

XAML:

<Window x:Class="CBSortTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    Height="300" Width="300">

    <Window.Resources>
        <CollectionViewSource Source="{Binding Path=CandyNames}" x:Key="cvs">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

    </Window.Resources>
    <StackPanel>
        <ComboBox ItemsSource="{Binding Source={StaticResource cvs}}" />
        <Button Content="Add" Click="OnAdd" />
    </StackPanel>
</Window>

Code behind:

后面的代码:

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;

namespace CBSortTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            CandyNames = new ObservableCollection<string>();

            OnAdd(this, null);
            OnAdd(this, null);
            OnAdd(this, null);
            OnAdd(this, null);

            DataContext = this;

            CandyNames.CollectionChanged += 
                (sender, e) =>
                {
                    CollectionViewSource viewSource =
                        FindResource("cvs") as CollectionViewSource;
                    viewSource.View.Refresh();
                };
        }

        public ObservableCollection<string> CandyNames { get; set; }

        private void OnAdd(object sender, RoutedEventArgs e)
        {
            CandyNames.Add("Candy " + _random.Next(100));
        }

        private Random _random = new Random();
    }
}