ComboBox ItemTemplate не работает для выбранного элемента при использовании ReactiveUI - PullRequest
0 голосов
/ 08 октября 2018

Я хочу заменить ComboBox ItemTemplate в xaml ниже на ReactiveUserControl.

<ComboBox Name="cmbColors" SelectionChanged="cmbColors_SelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Rectangle Fill="{Binding Name}" Width="16" Height="16" Margin="0,2,5,2" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

ComboBox.ItemTemplate применяется к раскрывающемуся списку и выбранному элементу. введите описание изображения здесь

ReactiveUI автоматически устанавливает свойство ItemTemplate.Это работает только для выпадающего списка, но не для выбранного элемента. введите описание изображения здесь

Вот мой ReactiveUserControl

// XAML
<StackPanel Orientation="Horizontal">
    <Rectangle x:Name="rect" Width="16" Height="16" Margin="0,2,5,2" />
    <TextBlock x:Name="text"/>
</StackPanel>

// View
public partial class TestColorboxV : ReactiveUserControl<TestColorboxVM>
{
    public TestColorboxV()
    {
        InitializeComponent();

        this.WhenActivated(d =>
        {
            this.OneWayBind(ViewModel,
                vm => vm.Val.Name,
                v => v.rect.Fill)
                .DisposeWith(d);

            this.OneWayBind(ViewModel,
                vm => vm.Val.Name,
                v => v.text.Text)
                .DisposeWith(d);
        });
    }
}

// ViewModel
public class TestColorboxVM : ReactiveObject
{
    private PropertyInfo _val;
    public PropertyInfo Val
    {
        get => _val;
        set => this.RaiseAndSetIfChanged(ref _val, value);
    }

    public TestColorboxVM(PropertyInfo val)
    {
        Val = val;
    }
}

И определение ComboBox в главном окне

<ComboBox x:Name="cmbColors" SelectionChanged="cmbColors_SelectionChanged">

Вот код, который использует выше ReactiveUserControl

// ViewModel
public class TestWindowVM : ReactiveObject
{
    public IList<TestColorboxVM> Items { get; set; }

    private TestColorboxVM _selectedItem;
    public TestColorboxVM SelectedItem
    {
        get => _selectedItem;
        set => this.RaiseAndSetIfChanged(ref _selectedItem, value);
    }

    public TestWindowVM() { }
}

// View
public partial class TestWindow : IViewFor<TestWindowVM>
{
    // Using a DependencyProperty as the backing store for ViewModel.  
    // This enables animation, styling, binding, etc.
    public static readonly DependencyProperty ViewModelProperty =
        DependencyProperty.Register("ViewModel", typeof(TestWindowVM), typeof(TestWindow), new PropertyMetadata(null));

    // Our main view model instance.
    public TestWindowVM ViewModel
    {
        get => (TestWindowVM)GetValue(ViewModelProperty);
        set => SetValue(ViewModelProperty, value);
    }

    // This is required by the interface IViewFor, you always just set it to use the 
    // main ViewModel property. Note on XAML based platforms we have a control called
    // ReactiveUserControl that abstracts this.
    object IViewFor.ViewModel
    {
        get => ViewModel;
        set => ViewModel = (TestWindowVM)value;
    }

    public TestWindow()
    {
        InitializeComponent();
        ViewModel = new TestWindowVM();

        this.WhenActivated(d =>
        {
            this.OneWayBind(ViewModel,
                vm => vm.Items,
                v => v.cmbColors.ItemsSource)
                .DisposeWith(d);

            this.Bind(ViewModel,
                vm => vm.SelectedItem,
                v => v.cmbColors.SelectedItem)
                .DisposeWith(d);
        });

        ViewModel.Items = typeof(Colors).GetProperties().Select(x => new TestColorboxVM(x)).ToList();
    }
}

Что я делаю не так?

...