使用 ReactiveUI wpf 无法将视图模型中的枚举值列表绑定到视图中的组合框 [英] Using ReactiveUI wpf unable to bind List of Enum values from viewmodel to combobox in view

查看:41
本文介绍了使用 ReactiveUI wpf 无法将视图模型中的枚举值列表绑定到视图中的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个枚举

 public enum LogType
     {
         None = 0,
         File = 1,
         Folder = 2
     }

我有这个组合框

    <ComboBox Name="CustomLogLogType"  FontSize="10"
              MinHeight="20" Height="20" SelectedItem="{Binding LogType}">

然后像这样的 ViewModel

Then a ViewModel like so

public class CustomLogRuleItemViewModel : ReactiveObject
{

    [Reactive]
    public LogType LogType { get; set; } = LogType.File;

    public List<LogType> LogTypes => Enum.GetValues(typeof(LogType)).Cast<LogType>().Where(_ => _ != LogType.None).ToList();
}

然后在视图后面的代码中

Then in code behind for the view

public partial class CustomLogRuleItemView : ReactiveUserControl<CustomLogRuleItemViewModel> 
{
    public CustomLogRuleItemView()
    {
        InitializeComponent();

        this.ViewModel = new CustomLogRuleItemViewModel();
        this.DataContext = this.ViewModel;

        //The below works
        //CustomLogLogType.ItemsSource = this.ViewModel.LogTypes;


        this.WhenActivated(
            disposables =>
            {

                //If I use below it will error with exception
                this.OneWayBind(this.ViewModel,
                        _ => _.LogTypes, _ => _.CustomLogLogType.ItemsSource)
                    .DisposeWith(disposables);
            });
    }
}

基本上如果我在下面绑定它就可以了

Basically if I bind with below it works

CustomLogLogType.ItemsSource = this.ViewModel.LogTypes;

但是如果我尝试使用 ReactiveUI 进行如下绑定

But if i try to use ReactiveUI to do the binding as in below

            this.OneWayBind(this.ViewModel,
                    _ => _.LogTypes, _ => _.CustomLogLogType.ItemsSource)
                .DisposeWith(disposables);

我收到一个异常,指出 ReactiveUI.IViewFor 上的 LogType 违反了类型T"的约束.不确定为什么我会对 IViewFor 产生争论,因为这与视图的 ViewModel 实现有关.

I get an exception stating that LogType on ReactiveUI.IViewFor violates the constraint of type 'T'. Unsure why I would be getting something arguing about IViewFor as that just has to do with the ViewModel implementation for view.

推荐答案

问题是 ReactiveUI 默认会通过解析 IViewFor 来设置 ItemTemplate旨在使您更容易拆分您的视图.因此,如果您在这些场景中添加 ItemTemplate、DisplayMemberPath 或 ItemTemplateSelector,自动视图查找将被关闭.请参阅 norelreferr> 用于执行此操作的代码行.

The issue is that ReactiveUI by default will set a ItemTemplate by resolving for IViewFor<TypeInItemsSource> which is meant to make it easier to split off your views. So the automatic view lookup gets turned off if you add a ItemTemplate, DisplayMemberPath or ItemTemplateSelector in these scenarios. See this code for the line of code that does it.

<ComboBox Name="CustomLogLogType"  FontSize="10"
                  MinHeight="20" Height="20">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

我可以通过添加自己的 ItemTemplate 来解决这个问题.

I was able to get around it by adding my own ItemTemplate.

我认为这实际上是一个错误,所以如果您不介意在 https 上打开一个错误://github.com/reactiveui/ReactiveUI/issues 我会在接下来的几天内修复它.我不认为我们应该为原始类型添​​加 ItemTemplate,因为实际上我不认为用户真的希望原始类型具有此功能.

I think this is realistically a bug though so if you don't mind opening a bug at https://github.com/reactiveui/ReactiveUI/issues I will put a fix in the next couple days for it. I don't think for primitive types we should be adding a ItemTemplate, since realistically I don't see users realistically wanting this feature for primitive types.

这篇关于使用 ReactiveUI wpf 无法将视图模型中的枚举值列表绑定到视图中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆