2013-03-23 122 views
0

有一個ComboBox,它是綁定到ObservableCollection的數據。一切都很好,直到這些傢伙想要分類的內容。我曾嘗試使用sortdescription,但這裏有些錯誤。請幫助 XAML排序下拉列表集合視圖wpf

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

    } 
+0

你檢查的回答? – Arshad 2013-03-23 04:26:36

回答

1

你缺少Items同時加入SortDescription,試試這個:

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

,並確保要排序基於項目DescriptionContent

+0

Collectionview不包含「Items」的定義 – user2201439 2013-03-23 07:25:55

+0

Collectionview不包含「Items」的定義 – user2201439 2013-03-23 07:34:45

+0

我試過了,但項目不是collectionview的一部分。 – user2201439 2013-03-25 01:21:20