In ItemsView
and subclasses of ItemsView
such as ListBox
, ComboBox
, ..., ReactiveUI automatically sets the ItemTemplate
property. This makes it easy to add a bunch of viewmodels to a list, bind the list to an ItemsView
and have the correct views for each viewmodel.
However, sometimes this behavior is not desirable. For example, after binding a list of regular objects to a ComboBox
I get the exception "Couldn't find view for MyNamespace.MyObject
".
I could fix this by setting the DisplayMemberPath
property on the ComboBox, however this property is not known at compile time. I want to bind DisplayMemberPath
from the ViewModel to the View. Unfortunately, this produces the following exception: "InvalidOperationException: Cannot set both DisplayMemberPath and ItemTemplate"
How can I fix this?
Set a dummy value for DisplayMemberPath
on the control and also keep the DisplayMemberPath
binding with the ViewModel. ReactiveUI will see the dummy value and will not set the ItemTemplate property. The dummy value will be replaced by the binding as soon as a ViewModel is attached and everything will work as intended.