WPF ComboboxAdaptor不显示ComboBox属性



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));
        }
    }
}

事实证明,我缺少将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>

最新更新