ObservableCollections icon indicating copy to clipboard operation
ObservableCollections copied to clipboard

WPF example is behaving weirdly

Open dgellow opened this issue 3 years ago • 4 comments

Hi there,

First let me thank you for the massive and impressive work you did with this library! Observable collections are really something that should get more love in the .Net ecosystem :)

I noticed something strange with the WPF example that you provide. When I run it locally and press a few times on the "Insert" button, I get the following :

image

The first 3 lines (in red) correspond to the initial items inserted via AddRange and is the tuple (value, view), while the rest (in green) has been added by pressing the "Insert" button and is only the value.

I do not understand why there is a difference, do you have any idea what could be the reason?

I'm also questioning yielding tuples (value, view) when enumerating, instead of only views. Tuples cannot be used directly for <ListView ItemSource={...} />. Is there a reason for this choice?

dgellow avatar Nov 19 '21 16:11 dgellow

SynchronizedView<T, TView> が通知する NotifyCollectionChangedEventArgs<T> が T 型のため WPF に渡される要素も T 型になっているのが原因のように思われます。

NotifyCollectionChangedEventHandler<TView> として TView 型コレクションの変更イベントにしてやるとうまくバインドされました。やっつけ実装

ビューが (T value, TView view) のタプルになっている理由はフィルタメソッドで両方見たいからではと推測しましたが、元コメントにあるように WPF で直接バインドできないのは不便に感じました。

※試してませんが NotifyCollectionChangedEventHandler<(T, Tvalue)> としたら Item2.~ で取れたりするのでしょうか。

-- In English (Google Translate)

It seems that the element passed to WPF is also T type because NotifyCollectionChangedEventArgs<T> notified by SynchronizedView<T, TView> is T type.

By modifying the type parameter to NotifyCollectionChangedEventHandler<TView>, it was successfully bound to a TView type collection change event. Rough implementation

I guess the reason the view is a tuple of (T value, TView view) is because it's convenient to be able to see both values in the filter method, but as the original comment says, it can't be bound directly in WPF. I felt inconvenient.

  • I haven't tried it, but if the type parameter is modified to NotifyCollectionChangedEventHandler<(T, Tvalue)>, can I get it with Item2.~ ?

yossiepon avatar Jan 31 '22 21:01 yossiepon

ありがとうございます、そのとおりですね、このままではあまりに不便というのはもっともです……! いただいた案も含め、ちょっと考えてみたいです。


You're right, it's too inconvenient as it is. I'll consider about it.

neuecc avatar Feb 01 '22 08:02 neuecc

こんな感じで書けるような、別の案を考えてみました。実装 google translate) I came up with another idea that could be written as:

ItemsView = list
        .ToSynchronizedCoupleView(x => $"({x}, {x}$)")	// IReadOnlyCollection<(T, TView)> -NotifyCollectionChangedEventHandler<(T, TView)>
        .ToSynchronizedSingleView()			// IReadOnlyCollection<TView> -NotifyCollectionChangedEventHandler<TView>
        .WithINotifyCollectionChanged();		// IReadOnlyCollection<TView>, INotifyCollectionChanged, INotifyPropertyChanged

yossiepon avatar Feb 19 '22 17:02 yossiepon

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jul 27 '22 00:07 github-actions[bot]

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Oct 26 '22 00:10 github-actions[bot]

Could we reopen this issue? It was stale but the issue still exists.

dgellow avatar Nov 14 '22 10:11 dgellow

sorry for no longer repond it, I'll check it.

neuecc avatar Nov 21 '22 03:11 neuecc