WPF ComboBoxAdaptor不显示ComboBox属性

时间:2018-02-20 18:31:22

标签: wpf xaml

https://stackoverflow.com/a/36192552/9387175

在此答案中,用户建议comboBoxAdaptor可用于将项目添加到组合框,即使它在项目源中不存在。我确实看到它在代码中工作,但我无法弄清楚为什么它拒绝显示。正常的组合框在下面的示例中正常运行,comboBoxAdaptor不可见。我错过了样式或模板之类的东西吗?我似乎找不到合适的组合。

我的xaml:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:adapters="clr-namespace:WpfApp1.Adapters"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="200"
        Width="650">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="210" />
            <ColumnDefinition Width="210" />
        </Grid.ColumnDefinitions>
        <adapters:ComboBoxAdaptor Grid.Column="0"
                                  AllowNull="False"
                                  Height="80"
                                  ItemsSource="{Binding Path=DataEntries}"
                                  SelectedItem="{Binding Path=DataEntry}">
            <ComboBox Height="80" />
        </adapters:ComboBoxAdaptor>
        <ComboBox Grid.Column="1"
                  Height="80"
                  ItemsSource="{Binding Path=DataEntries}"
                  SelectedItem="{Binding Path=DataEntry}"
                  DisplayMemberPath="Name"
                  SelectedValuePath="Name" />
    </Grid>
</Window>

我的代码:

using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SampleViewModel vm = new SampleViewModel();
            DataContext = vm;
        }
    }

    public class SampleDataClass
    {
        public string Name { get; set; }

        public SampleDataClass(string name)
        {
            Name = name;
        }
    }

    public class SampleViewModel : INotifyPropertyChanged
    {
        private readonly IList<SampleDataClass> _dataEntries;
        private string _dataEntry;
        public event PropertyChangedEventHandler PropertyChanged;

        public SampleViewModel()
        {
            IList<SampleDataClass> list = new List<SampleDataClass>();
            list.Add(new SampleDataClass("tools"));
            list.Add(new SampleDataClass("set"));
            list.Add(new SampleDataClass("sort"));
            list.Add(new SampleDataClass("flap"));
            _dataEntries = list;
        }

        public IList<SampleDataClass> DataEntries
        {
            get { return _dataEntries; }
        }

        public string DataEntry
        {
            get
            {
                return _dataEntry;
            }
            set
            {
                if (_dataEntry == value) {return;}

                _dataEntry = value;
                OnPropertyChanged("DataEntry");
            }
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

原来我错过了将ComboBox链接到ContentControl(ComboBoxAdaptor)内容的样式

样式示例

<Style TargetType="adapters:ComboBoxAdaptor">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="adapters:ComboBoxAdaptor">
                    <ContentPresenter Content="{TemplateBinding ComboBox}"
                                      Margin="{TemplateBinding Padding}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>