Using ReactiveUI wpf unable to bind List of Enum values from viewmodel to combobox in view

跟風遠走 提交于 2019-12-24 19:08:31

问题


Lets say I have this enum

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

I have this ComboBox in view

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

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;

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

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

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.


回答1:


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>

I was able to get around it by adding my own 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.



来源:https://stackoverflow.com/questions/56778695/using-reactiveui-wpf-unable-to-bind-list-of-enum-values-from-viewmodel-to-combob

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!