2012-02-15 4 views
0

コントロールのデータソースを、観測可能なキー値のコレクションに設定したいが、そのペアのキー部分のみを表示したい。WPFコントロールでバインドを正しく設定する

私は第三者のマルチセレクションコンボボックスを持っています。

キー値ペアのコレクションを保持するように、付属のデータソースクラスを変更しました。ここで

は、クラスである:私の背後にあるコードで

public class DataSource : INotifyPropertyChanged 
    { 
     private ObservableCollection<KeyValuePair<string,string>> _items; 

     public DataSource(ObservableCollection<KeyValuePair<string, string>> items) 
     { 
      _items = items; 
     } 

     #region INotifyPropertyChanged Members 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     #endregion 




     public ObservableCollection<KeyValuePair<string, string>> Items 
     { 
      get { return _items; } 
     } 

     private string _selectedItem = ""; 
     public string SelectedItem 
     { 
      get { return _selectedItem; } 
      set 
      { 
       _selectedItem = value; 
       OnPropertyChanged("SelectedItem"); 
      } 
     } 

     private ObservableCollection<KeyValuePair<string, string>> _selectedItems; 
     public ObservableCollection<KeyValuePair<string, string>> SelectedItems 
     { 
      get 
      { 
       if (_selectedItems == null) 
       { 
        _selectedItems = new ObservableCollection<KeyValuePair<string, string>> { new KeyValuePair<string,string>("ALL"," ") }; 
        SelectedItemsText = WriteSelectedItemsString(_selectedItems); 
        _selectedItems.CollectionChanged += 
         (s, e) => 
         { 
          SelectedItemsText = WriteSelectedItemsString(_selectedItems); 
          OnPropertyChanged("SelectedItems"); 
         }; 
       } 
       return _selectedItems; 
      } 
      set 
      { 
       _selectedItems = value; 
      } 
     } 

     public string SelectedItemsText 
     { 
      get { return _selectedItemsText; } 
      set 
      { 
       _selectedItemsText = value; 
       OnPropertyChanged("SelectedItemsText"); 
      } 
     } string _selectedItemsText; 


     private static string WriteSelectedItemsString(IList<KeyValuePair<string, string>> list) 
     { 
      if (list.Count == 0) 
       return String.Empty; 

      StringBuilder builder = new StringBuilder(list[0].Key); 

      for (int i = 1; i < list.Count; i++) 
      { 
       builder.Append(", "); 
       builder.Append(list[i]. 
); 
      } 

      return builder.ToString(); 
     } 
    } 

ています

DataSource ds = new DataSource(materialNames); 
      cmbLastEditors.DataContext = ds; 

そして、私のXAMLで私が持っている:

<my1:MultiComboBox Name="cmbLastEditors" Grid.Row="8" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" SelectionMode="Multiple" DisplaySeparator=", " ItemsSource="{Binding Items}" SelectedItems="{Binding SelectedItems}" /> 

を私のような何かをしたいです

ItemsSource="{Binding Items.Key}" 

誰でも助けてくれますか?これ以上情報が必要な場合はお知らせください。

答えて

3

通常のコンボボックスでは、ValueMemberフィールドとDisplayMemberフィールドがあります。 DisplayMemberPathと同じように機能します。より良い説明のためのthisのリンクを参照してください、しかし、あなたのコンボボックスには、次のようになります。もちろん

<my1:MultiComboBox Name="cmbLastEditors" 
        Grid.Row="8" Grid.Column="1" 
        VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
        SelectionMode="Multiple" DisplaySeparator=", " 
        ItemsSource="{Binding Items}" 
        SelectedItems="{Binding SelectedItems}" 
        DisplayMemberPath="Key" /> 

これはあなたのサードパーティ製のコンボボックスで動作する場合、私はわかりません。

これが機能しない場合は、ComboboxのItemTemplateでバリューコンバータを使用できます。たとえば、ItemTemplateのテキストブロックにコンバーターを追加します。ここでオブジェクト全体をバインドし、コンバーターを使用して表示するキー値を返します。

class MyKeyValueConverter : IValueConverter 
{ 
    public object Convert(object aValue) 
    { 
     var pair = aValue as KeyValuePair<string, string>; 
     return pair.Key; 
    } 
} 


<TextBlock Text="{Binding Path=., Converter={StaticResource myKeyValueConverter}}"/> 
+0

ブリリアント。ありがとうございます:D DisplayMemberPath = "Key"を追加すると、大事なことが起こりました。 – user589195

4

これを行うには、ItemTemplateを使用してリスト内の各アイテムをレンダリングします。

<my1:MultiComboBox ...> 
    <my1:MultiComboBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Key}"/> 
     </DataTemplate> 
    <my1:MultiComboBox.ItemTemplate> 
</my1:MultiComboBox> 

つまり、カスタムMultiComboBoxが通常のWPF ItemTemplateセマンティクスをサポートしていると仮定します。

+0

+1 DataTemplateがすでに使用されていると思ったので、私の考えよりも実際には優れています。時々私は必要以上に複雑だと思う:) – dowhilefor

+0

実際、私はValueMember/DisplayMemberを忘れていた。カスタムコントロールがそれをサポートしていれば、それは簡単です。 +1周りに:) –

関連する問題