簡體   English   中英

如何將ItemsSource綁定到DataTemplate中的ComboBox

[英]How to bind ItemsSource to ComboBox within a DataTemplate

ItemsSource綁定到列表似乎工作正常,除了在DataTemplate內部時。 我現在有3個ComboBox:2個位於DataTemplate內,其中一個具有可工作的硬編碼項目,另一個具有不可用的ItemsSource設置。 最后一個在DataTemplate外部,並且正在使用ItemsSource。 2個組合框列表和1個組合框

我試圖修改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"); }
        }
    }

這是我的DataContext

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>

這是具有硬編碼值的ComboBox,它可以正常工作

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”屬性。 BindingExpression:Path = ItemsList; DataItem ='ListEntry'(HashCode = 20917673); 目標元素是'ComboBox'(Name =''); 目標屬性為“ ItemsSource”(類型為“ IEnumerable”)

System.Windows.Data錯誤:40:BindingExpression路徑錯誤:在“對象”“ ListEntry”(HashCode = 52252659)上找不到“ ItemsList”屬性。 BindingExpression:Path = ItemsList; DataItem ='ListEntry'(HashCode = 52252659); 目標元素是'ComboBox'(Name =''); 目標屬性為“ ItemsSource”(類型為“ IEnumerable”)

任何想法有什么問題嗎? 我的其他綁定正在工作,所以我不認為DataContext是錯誤的(它在MainWindow.cs文件中設置:DataContext = this.dataClass;)

在數據模板中創建控件時,控件的數據上下文不使用窗口的上下文,而是使用模板所使用的控件的上下文。因此,當列表框顯示ListEntitys列表時,組合框將嘗試從每個ListEntity獲取ItemsList屬性-該屬性不存在

解決此問題的一種方法是使組合框使用窗口上下文:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM