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

Dom*_*nas 1 c# data-binding wpf reactiveui

ViewModel是一个

public List<string> OperationModes { get; } = Enum.GetNames(typeof(EOperationMode)).ToList();
Run Code Online (Sandbox Code Playgroud)

我想绑定到ComboBox.

反应式UI方式| 不工作

this.OneWayBind(ViewModel, model => model.OperationModes, window => window.ComboBoxOperationMode.ItemsSource).DisposeWith(r);
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如果使用reactiveUI将 绑定listComboBox,我会在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.
Run Code Online (Sandbox Code Playgroud)

xaml 方式 | 在职的

<ComboBox
    x:Name="ComboBoxOperationMode"
    ItemsSource="{Binding ViewModel.OperationModes}"/>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我怎么解决这个问题?或者无法绑定list<string>via reactiveUI


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

mm8*_*mm8 5

设置 的DisplayMemberPath属性ComboBox以避免使用ViewModelViewHost试图解析 的视图的string

<ComboBox x:Name="ComboBoxOperationMode" DisplayMemberPath="." />
Run Code Online (Sandbox Code Playgroud)

  • 您还可以设置一个项目模板来避免该问题。因为如果您绑定到 ItemsSource 并且不设置任何一个,它会尝试为列表中的类型查找 IViewFor 。 (2认同)