RX:如果绑定到list <string> | ComboBox为空ViewModelViewHost找不到视图模型的有效视图

时间:2019-04-24 08:31:32

标签: c# wpf data-binding reactiveui

ViewModel中是

public List<string> OperationModes { get; } = Enum.GetNames(typeof(EOperationMode)).ToList();

我想绑定到ComboBox上。

reactiveUI方式|不起作用

this.OneWayBind(ViewModel, model => model.OperationModes, window => window.ComboBoxOperationMode.ItemsSource).DisposeWith(r);

enter image description here

如果使用reactiveUIlist绑定到ComboBox,则会在console output中收到以下错误。

控制台输出

DefaultViewLocator: Failed to resolve view for view model type 'System.Object'.
DefaultViewLocator: Failed to resolve view for view model type 'System.Object'.
ViewModelViewHost: The ViewModelViewHost could not find a valid view for the view model of type System.String and value Passthrough.

xaml方式|工作

<ComboBox
    x:Name="ComboBoxOperationMode"
    ItemsSource="{Binding ViewModel.OperationModes}"/>

enter image description here

如何解决此问题?还是无法通过list<string>绑定reactiveUI


Github问题:https://github.com/reactiveui/ReactiveUI/issues/2008

2 个答案:

答案 0 :(得分:4)

设置DisplayMemberPath的{​​{1}}属性,以避免使用试图解析ComboBox的视图的ViewModelViewHost

string

答案 1 :(得分:0)

由于列表是只读的,因此您可以直接分配ItemsSource而不需要OneWayBind(通过代码或通过xaml,就像您已经拥有的那样)。 ReactiveUI绑定功能强大,但需要付出一定的代价,因此,如果您可以直接进行分配,那就去做吧。即使列表随时间变化,您仍然可以借助ObservableCollection而不是列表来避免ReactiveUI绑定。