RX:如果绑定到列表 <string>,组合框为空 | ViewModelViewHost 找不到视图模型的有效视图

RX: ComboBox empty if bind to list<string> | ViewModelViewHost could not find a valid view for the view model

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);

如果使用 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}"/>

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


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

由于您的列表是只读的,您可以直接分配 ItemsSource 而无需 OneWayBind(通过代码或通过 xaml 就像您已经拥有的那样)。 ReactiveUI 绑定很强大,但它是有代价的,所以如果你可以摆脱直接赋值,那就去做吧。即使您的列表随时间发生变化,您仍然可以借助 ObservableCollection 而不是列表来避免 ReactiveUI 绑定。

设置 ComboBoxDisplayMemberPath 属性 以避免使用试图为 string 解析视图的 ViewModelViewHost:

<ComboBox x:Name="ComboBoxOperationMode" DisplayMemberPath="." />