5

バックエンド クラスで 2 つのリストを使用しています。各リストは異なるタイプです。ユーザーに単一のリスト (両方のリストの結合を含む) を提示したいと考えています。このリスト内の項目が選択されると、その項目の詳細が表示されます。

コードは次のようになります。

私のバックエンドクラスは次のようになります

public ObservableCollection<Person> People {get;}
public ObservableCollection<Product> Products {get;}

私のXAMLは次のようになります

<ListBox x:Name="TheListBox" ItemsSource={Some Expression to merge People and Products}>
   <ListBox.Resources>
         People and Product Data Templates
   </ListBox.Resources>
</ListBox>
      ...
<ContentControl Content={Binding ElementName=TheListBox, Path=SelectedItem }>
   <ContentControl.Resources>
         Data Templates for showing People and Product details
   </ContentControl.Resources>
</ContentControl>

助言がありますか?

4

3 に答える 3

10

これにはCompositeCollectionを使用できます。この質問を見てください。

于 2011-02-14T07:31:11.507 に答える
2

ViewModel でこのようなプロパティを公開しない理由がわかりません。

ObservableCollection<object> Items 
{
  get 
  {
    var list = new ObservableCollection<object>(People);
    list.Add(Product);
    return list;
  }
}

そして、あなたのxamlでこれを行います:

<ListBox x:Name="TheListBox" ItemsSource={Binding Items}>
   <ListBox.Resources>
         People and Product Data Templates
   </ListBox.Resources>
</ListBox>
      ...
<ContentControl Content={Binding ElementName=TheListBox, Path=SelectedItem }>
   <ContentControl.Resources>
         Data Templates for showing People and Product details
   </ContentControl.Resources>
</ContentControl>

アップデート:

モデルを別の方法で操作する必要がある場合は、次の手順を実行します。

ObservableCollection<object> _Items 
ObservableCollection<object> Items 
{
  get 
  {
    if (_Items == null)
    {
      _Items = new ObservableCollection<object>();
      _Items.CollectionChanged += EventHandler(Changed);
    }
    return _Items;
  }
  set 
  { 
    _Items = value;
    _Items.CollectionChanged += new CollectionChangedEventHandler(Changed);
  }
}

void Changed(object sender,CollectionChangedEventArgs e)
{
  foreach(var item in e.NewValues)
  {
    if (item is Person)
      Persons.Add((Person)item);
    else if (item is Product)
      Products.Add((Product)item);
  }
}

これはほんの一例です。ただし、上記をニーズに合わせて変更すると、目標に到達する可能性があります

于 2010-07-15T12:42:35.573 に答える
0

ここで、ほとんどの方法で得たブログ投稿を見つけました。作成者の AggregateCollection と multivalueconverter を使用して、作業を完了しました。

于 2010-07-14T20:58:13.110 に答える