WPF: How to bind to only one item in a collection, not using ItemsControl since I don't want to display all of them

前端 未结 3 1067
-上瘾入骨i
-上瘾入骨i 2020-12-09 17:14

I have this requirement, that I have a collection of items (ObservableCollection), but I only want to display the first item. The requirement comes from the fact that in mos

3条回答
  •  星月不相逢
    2020-12-09 17:39

    Ok, late to the party but I thought I'd share my 2 cents anyway: I'd better go with a dumber (XAML-)view and a view-model closer to your presentation needs.

    Translated: instead of mapping your existing view-model (or raw data) and its collection of items directly to the view, I suggest to map that to an appropriate view-model showing something like a YourItemViewModel FirstItem property and a bool HasMore property. That second view-model would be easily unit-testable to make sure it behaves propertly, and would be easily mapped to a view with less logic, so to avoid possible hard-to-test problems in view.

提交回复
热议问题