【问题标题】:WPF ComboBoxAdaptor does not display the ComboBox propertyWPF ComboBoxAdaptor 不显示 ComboBox 属性
【发布时间】:2018-02-20 18:31:22
【问题描述】:

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

在此答案中,用户建议可以使用 comboBoxAdaptor 将项目添加到组合框中,即使项目源中不存在该项目。事实上,我确实看到它在代码中工作,但我不知道为什么它拒绝显示。正常的组合框在下面的示例中正常工作,comboBoxAdaptor 不可见。我是否缺少样式或模板之类的东西?我似乎找不到合适的组合。

我的xml:

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

【问题讨论】:

  • 尝试将 SelectedItem 更改为 SelectedItem="{Binding SelectedProposal.CategoryCode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}}" 您也需要实现 INotifyPropertyChanged。
  • @dev1998 绑定一切正常,我可以在调试和窥探时看到所有内容。我只是有一个空白的正方形可以直观地查看。

标签: wpf xaml


【解决方案1】:

原来我缺少将 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>

【讨论】:

    猜你喜欢
    • 2014-11-05
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2021-10-13
    • 2014-05-30
    相关资源
    最近更新 更多