0

有一个数据绑定到 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);

    }
4

1 回答 1

1

Items在添加时丢失了SortDescription,试试这个:

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

并确保您要根据Description或对项目进行排序Content

于 2013-03-23T04:23:19.060 回答