【问题标题】:How to bind ItemsSource to ComboBox within a DataTemplate如何将 ItemsSource 绑定到 DataTemplate 中的 ComboBox
【发布时间】:2019-12-11 03:27:32
【问题描述】:

ItemsSource 绑定到列表似乎工作正常,除非在 DataTemplate 中。 我现在有 3 个 ComboBoxes:2 个位于 DataTemplate 内,其中一个具有正在工作的硬编码项目,另一个具有不工作的 ItemsSource 集。最后一个在 DataTemplate 之外,正在使用 ItemsSource。 2 lists of ComboBoxes and 1 ComboBox

我尝试修改 DataContext、RelativeSource 和 ElementName,但没有任何运气。

ItemsSource 列表包含 ListEntry

public class ListEntry : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private string m_name;
        private string m_desc;

        public ListEntry(string name, string desc)
        {
            Name = name;
            Desc = desc;
        }

        public string Name
        {
            get { return m_name; }
            set { m_name = value; NotifyPropertyChanged("Name"); }
        }
        public string Desc
        {
            get { return m_desc; }
            set { m_desc = value; NotifyPropertyChanged("Desc"); }
        }
    }

这是我的数据上下文

public class DataClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private ObservableCollection<ListEntry> m_itemsList;

        public ObservableCollection<ListEntry> ItemsList
        {
            get { return m_itemsList; }
            set { m_itemsList = value; NotifyPropertyChanged("ItemsList"); }
        }

    }

这是带有 ItemsSource 的 ComboBox

XAML
<Window.Resources>
        <DataTemplate x:Key="DataTempItemsSource">
            <ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding ItemsList}" DisplayMemberPath="Name"
                  SelectedValuePath="Name" SelectedIndex="0"/>
        </DataTemplate>

<ListBox HorizontalAlignment="Center" 
                 VerticalAlignment="Center"
                 ItemTemplate="{StaticResource DataTempItemsSource}"
                 ItemsSource="{Binding ItemsList}">
        </ListBox>

这是具有硬编码值的组合框,它应该正常工作

XAML
<DataTemplate x:Key="DataTempHardCode">
            <ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" SelectedIndex="0">
                <ComboBoxItem Content="One"/>
                <ComboBoxItem Content="Two"/>
            </ComboBox>
        </DataTemplate>

<ListBox HorizontalAlignment="Center" 
                 VerticalAlignment="Center"
                 ItemTemplate="{StaticResource DataTempHardCode}"
                 ItemsSource="{Binding ItemsList}">
        </ListBox>

我还确认带有 ItemsSource 的 ComboBox 在 DataTemplate 之外工作。

<ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding ItemsList}" DisplayMemberPath="Name"
                  SelectedValuePath="Name" SelectedIndex="0"/>

我得到了这两个错误: System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“ListEntry”(HashCode=20917673)上找不到“ItemsList”属性。绑定表达式:路径=项目列表; DataItem='ListEntry' (HashCode=20917673);目标元素是 'ComboBox' (Name='');目标属性是“ItemsSource”(类型“IEnumerable”)

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“ListEntry”(HashCode=52252659)上找不到“ItemsList”属性。绑定表达式:路径=项目列表; DataItem='ListEntry' (HashCode=52252659);目标元素是 'ComboBox' (Name='');目标属性是“ItemsSource”(类型“IEnumerable”)

任何想法有什么问题吗?我的其他绑定正在工作,所以我不认为 DataContext 是错误的(它在 MainWindow.cs 文件中设置:DataContext = this.dataClass;)

【问题讨论】:

  • 为什么你想要一个项目列表,其中每个项目都显示为所有项目的组合框?
  • 因为我有一个带有 ListBoxItems 的 ListBox,您可以在其中从 DropDown 菜单(ComboBox)中分别在每个项目上选择不同的选项。 ListBoxItems 中有更多元素,它们只是不包含在我的问题中。顺便问下好问题!

标签: c# .net wpf xaml


【解决方案1】:

当您在数据模板中创建控件时,控件的数据上下文不使用窗口的上下文,它使用的是模板所在控件的上下文。 因此,当您的列表框显示 ListEntitys 列表时,组合框会尝试从每个 ListEntity 获取 ItemsList 属性 - 这不存在

解决此问题的一种方法是让您的组合框使用窗口上下文:

ItemsSource="{Binding Path=DataContext.ItemsList, RelativeSource={RelativeSource AncestorType=Window}}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 2018-08-02
    • 2017-01-05
    • 2015-01-17
    • 2013-03-23
    • 2018-11-08
    相关资源
    最近更新 更多