排序下拉列表集合视图wpf

本文关键字:wpf 视图 集合 下拉列表 排序 | 更新日期: 2023-09-27 18:05:38

有一个绑定到ObservableCollection的ComboBox一切都很顺利,直到他们想把内容分类。我试过使用sortdescription,但是这里有问题。请帮助XAML

<ComboBox Margin="10,0,0,0" FontSize="16" Height="30" Width="200" 
                                        ItemsSource="{Binding Path=NationalityDropDownList}" 
                                        DisplayMemberPath="Description"
                                        SelectedValuePath="Code" 
                                        SelectedIndex="{Binding Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Path=SelectedIndex}"
                                        SelectedValue="{Binding Path=SelectedNationality, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Text="0">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="SelectionChanged">
                                        <i:InvokeCommandAction Command="{Binding UpdateNationalityCodeCommand}" 
                                                CommandParameter="{Binding Path=SelectedIndex}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>

CODE BEHIND

/// <summary>
        /// NationalityDropDownList 
        /// </summary>
        private CollectionView nationalityDropDownList;
        /// <summary>
        /// Gets or Sets the NationalityDropDownList
        /// </summary>
        public CollectionView NationalityDropDownList
        {
            get
            {
                SortDescription sd = new SortDescription("Description", ListSortDirection.Ascending);
                nationalityDropDownList.SortDescriptions.Add(sd);
                return nationalityDropDownList;
            }

            set
            {
                if (NationalityDropDownList != value)
                {
                    nationalityDropDownList = value;
                    RaisePropertyChanged(() => NationalityDropDownList);
                }
            }
        }

        /// <summary>
    /// Updates the Nationality Drop Down List information
    /// </summary>
    private void UpdateNationalityDropDownList()
    {
        // TODO: Currently hardcoded for Focus Group
        IList<Nationality> list = new List<Nationality>();
        Nationality nationality = new Nationality();
        // TODO : This code should go through the hibernate Code table to read the list of Nationality.
        IEnumerable<Code> nationalityList = localCodeLibrary.FindAllNationalities();
        nationality.Code = string.Empty;
        nationality.Description = "";
        list.Add(nationality);
        foreach (Code national in nationalityList)
        {
            nationality.Code = national.Id.Code;
            nationality.Description = national.Description;
            list.Add(nationality);
        }
        //nationalityDropDownList.SortDescriptions.Add(new SortDescription(nationality.Description, ListSortDirection.Ascending));
        NationalityDropDownList = new CollectionView(list);
    }

排序下拉列表集合视图wpf

您在添加SortDescription时缺少Items,请尝试:

 SortDescription sd = new SortDescription("Description", ListSortDirection.Ascending);
 nationalityDropDownList.Items.SortDescriptions.Add(sd);

并确保您要根据DescriptionContent对项目进行排序