在ViewModel
中是
public List<string> OperationModes { get; } = Enum.GetNames(typeof(EOperationMode)).ToList();
我想绑定到ComboBox
上。
this.OneWayBind(ViewModel, model => model.OperationModes, window => window.ComboBoxOperationMode.ItemsSource).DisposeWith(r);
如果使用reactiveUI
将list
绑定到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.
<ComboBox
x:Name="ComboBoxOperationMode"
ItemsSource="{Binding ViewModel.OperationModes}"/>
如何解决此问题?还是无法通过list<string>
绑定reactiveUI
?
Github问题:https://github.com/reactiveui/ReactiveUI/issues/2008
答案 0 :(得分:4)
设置DisplayMemberPath
的{{1}}属性,以避免使用试图解析ComboBox
的视图的ViewModelViewHost
:
string
答案 1 :(得分:0)
由于列表是只读的,因此您可以直接分配ItemsSource而不需要OneWayBind(通过代码或通过xaml,就像您已经拥有的那样)。 ReactiveUI绑定功能强大,但需要付出一定的代价,因此,如果您可以直接进行分配,那就去做吧。即使列表随时间变化,您仍然可以借助ObservableCollection而不是列表来避免ReactiveUI绑定。