0

他の 2 つのオブジェクトから作成された ObservableCollection をバインドしようとしたときに、いくつかの問題が発生しました。最初に ActiveSketchs をバインドする必要があります。次に、スケッチ (ActivesSketchs.Union(InactiveSketchs) です) を個別にバインドする必要があります。

以下のコードは動くと思っていたのですが、うまくいきません。ActiveSketch バインディングは正しく機能しますが、Sketchs バインディングは機能しません:

    private ObservableCollection<Sketch> _sketchs;
    public ObservableCollection<Sketch> Sketchs
    {
        get { return _sketchs = new ObservableCollection<Sketch>(ActiveSketchs.Union(InactiveSketchs)); }
        set { _sketchs = value; }
    }

    private ObservableCollection<Sketch> _activeSketchs;
    public ObservableCollection<Sketch> ActiveSketchs
    {
        get { return _activeSketchs; }
        set { _activeSketchs = value; }
    }

    private ObservableCollection<Sketch> _inactiveSketchs;
    public ObservableCollection<Sketch> InactiveSketchs
    {
        get { return _inactiveSketchs; }
        set { _inactiveSketchs = value; }
    }

ソースアイテムを設定する方法は次のとおりです。

    HeadbandRight.ItemsSource = Sketchs;
    HeadbandLeft.ItemsSource = Sketchs;
    MainScatterViewer.ItemsSource = ActiveSketchs;
4

2 に答える 2

0

これは私には意味がありません

private ObservableCollection<Sketch> _sketchs;
public ObservableCollection<Sketch> Sketchs
{
    get { return _sketchs = new ObservableCollection<Sketch>(ActiveSketchs.Union(InactiveSketchs)); }
    set { _sketchs = value; }
}

getがユニオンの場合、なぜセットがあるのですか?

getがある場合、なぜObservableCollectionである必要があるのですか?
個々のOCを変更しても、結合されたコレクションで変更されたコレクションは起動されません。
新規化することで結合されたOCを返すことができますが、oc1またはoc2に変更しても、UIが更新することを認識できるようにコレクションが変更されたOCcombは起動されません。
結合されたものに対して手動でNotifyPropertyChangedを起動する必要があるため、IEnumerableを返すこともできます。

    private ObservableCollection<string> oc1 = new ObservableCollection<string>();
    private ObservableCollection<string> oc2 = new ObservableCollection<string>();
    public MainWindow()
    {
        oc1.Add("one");
        oc1.Add("two");
        oc2.Add("three");
        oc2.Add("four");
        this.DataContext = this;
        InitializeComponent();
    }
    public IEnumerable<string> IEcomb { get { return oc1.Union(oc2); } }
    public ObservableCollection<string> OCcomb { get { return new ObservableCollection<string>(oc1.Union(oc2)); } }
于 2013-01-11T14:43:23.043 に答える
0

リストからフィルタリングされたアイテムを取得するには、 CollectionViewSourceを使用する必要があります。

public InitFunction()
{
    this.Sketchs = new ObservableCollection<Sketch>();

    this.ActiveSketchs = new CollectionViewSource();
    this.ActiveSketchs.Source = this.Sketches ;
    this.ActiveSketchs.Filter += (s, e) =>
    {
        var sketch = e.Item as Sketch;

        e.Accepted = sketch.IsActive; // or whatever test you need
    };

    HeadbandRight.ItemsSource = Sketchs;
    HeadbandLeft.ItemsSource = Sketchs;
    MainScatterViewer.ItemsSource = ActiveSketchs.View;
}

public ObservableCollection<Sketch> Sketchs { get; set; }

public CollectionViewSource ActiveSketchs { get; set; }

完全な例はこちら: http://uicraftsman.com/blog/2010/10/27/filtering-data-using-collectionviewsource/

于 2013-01-11T16:07:12.550 に答える