Using ReactiveUI wpf unable to bind List of Enum v

2019-08-20 04:52发布

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条回答
乱世女痞
2楼-- · 2019-08-20 05:08

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.

查看更多
登录 后发表回答