2016-10-25 16 views
3

私はコンボボックスを持っています。このコンボボックス内のアイテムはデータベースから取得しており、アルファベット順にソートしようとしていますが、実行できません。誰かがこれを達成する方法を考え出すのを手伝ってもらえますか?wpfアルファベット順のコンボボックスのソート

private ObservableCollection<StudentModules> modules; 

public StudentModule() 
     { 
      InitializeComponent(); 
      DataContext = this; 
      Modules = new ObservableCollection<StudentModules>(); 
      ModuleNames.ItemsSource = modules; 

      IDataAccess<ModulesFinder, StudentModules> moduleRetriever = ((IDataManager)Application.Current.Properties["Database”]).GetDataAccessor<ModuleFinder, StudentModules>(); 

      foreach (StudentModules module in retrieve.AllItems()) 
      { 
       Modules.Add(module); 
      } 
     } 

<ComboBox Name="ModuleNames" > 
        <ComboBox.SelectedItem> 
         <Binding Path="ModuleDetails" NotifyOnValidationError="True"> 
          <Binding.ValidationRules> 
           <validators:IsMandatoryValidation FieldName="Module Names"/> 
          </Binding.ValidationRules> 
         </Binding> 
        </ComboBox.SelectedItem> 
       </ComboBox> 
+1

これまでに試したことを表示できますか? –

+0

それはあなたの質問にwinformまたはwpfまたは他の種類のアプリケーションがあるかどうかを言及する必要があります –

+0

XAMLの存在によって判断する@viveknuna WPF – ChrisF

答えて

2

CollectionViewSourceを使用してください。

XAML:

<UserControl.Resources> 
    <CollectionViewSource x:Key="ModulesViewSource" Source="{Binding Path=Modules}"> 
    <CollectionViewSource.SortDescriptions> 
     <scm:SortDescription PropertyName="Name" /> 
    </CollectionViewSource.SortDescriptions> 
    </CollectionViewSource> 
</UserControl.Resources> 

... 

<ComboBox ItemsSource="{StaticResource ModulesViewSource}" /> 

注:SCMとしてSystem.ComponentModel名前空間をインポートする必要があります。

+0

のSCMがそれを見た? – luka

+0

これはこれですか? xmlns:scm = "clr-namespace:System.ComponentModel; assembly = WindowsBase" – luka

+1

@luka:私の答えは更新されましたが、** scm **はSystem.ComponentModelを参照しています。 –

関連する問題