【问题标题】:How can I bind ComboBox ItemSource in a DataTemplate to a List?如何将 DataTemplate 中的 ComboBox ItemSource 绑定到列表?
【发布时间】:2015-01-17 09:01:23
【问题描述】:

我对 WPF、C#、XAML 等非常陌生。但我正在学习。到目前为止,我无法将 DataTemplate 中的 ComboBox ItemSource 绑定到 C# 字符串列表。

这是我的 C# 代码:

public class Giraffe {
    ...
    public Zebra() {
        AnimalStuff.Add("Head");
        AnimalStuff.Add("Stripes");
        AnimalStuff.Add("Tail");
    }

    private List<string> _animalStuff= new List<string>();
    public List<string> AnimalStuff {
        get { return _animalStuff; }
        set {
            _animalStuff= value;
            OnPropertyChanged("AnimalStuff");
        }
    }

    #region OnPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName) {
        if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
    }
    #endregion
}

这是我的 XAML 代码:

...
<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="AnimalTemplates.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>
...
<ComboBox ItemsSource="{Binding AnimalStuff}" />

那部分工作得很好。我得到一个组合框,其中包含所有 AnimalStuff 字符串的下拉列表。

但是,我需要将该组合框放在 DataTemplate 中。我无法让绑定工作。这是 DataTemplate 文件:

...
<DataTemplate x:Key="AnimalTemplate">
    <Grid x:Name="GridAnimalTemplate" >
        ...
        <Label Content="Select Animal Part:" />

        <ComboBox ItemsSource="{Binding AnimalStuff}" />
    </Grid>
</DataTemplate>

我将此添加到 XAML 文件中:

<ListBox ItemTemplate="{DynamicResource AnimalTemplate}" />

在 ListBox 中,我得到了正确的标签和组合框。但是组合框只是空的,因为它找不到要绑定的 AnimalStuff 列表。我尝试将绑定 RelativeSource 设置为各种设置,但我似乎无法让它在 C# 代码中找到列表。

有什么想法吗?

【问题讨论】:

  • ListBox 的ItemsSource 是什么?
  • 我没有。当我尝试添加它时,我收到此消息:使用 ItemsSource 之前,Items 集合必须为空。
  • 这意味着您为您的 ListBox 手动添加了一些项目,那么它们是什么?
  • 我有这段代码让 ListBox 出现在代码中: .Items.Add(new Object());删除它允许它运行。但是如何为具有多个控件的 ListBox 创建 ItemsSource?

标签: c# wpf xaml binding datatemplate


【解决方案1】:

先添加INotifyPropertyChanged的接口,看看后面会发生什么。

如果不工作,请检查以下link

【讨论】:

    【解决方案2】:

    根据您将项目添加到列表的时间以及将列表设置为ItemsSource 的时间,ComboBox 可能不会反映对ItemsSource 的更新。使用ObservableCollection 而不是List,看看是否能解决您的问题。

    【讨论】:

    • 我试过了,但没有用。请参阅我接受的答案。但感谢您的宝贵时间。
    【解决方案3】:

    King King 让我走上了正轨。

    这是正确答案:在 C# 代码中,您需要创建一个新列表。

    List<AnimalListboxItem>[ AnimalItems = new List<AnimalListboxItem>();
    public class AnimalListboxItem{
        public string AnimalPartLabel { get; set; }
        public List<string> AnimalStuff{ get; set; }
    }
    

    然后使用该新列表作为列表框的项目源:

    AnimalItems = new List<AnimalListboxItem>();
    <listbox name>.Items.Add(new AnimalListboxItem
        AnimalPartLabelBound = "Select Animal Part:",
        AnimalStuffBound = AnimalStuff);
    

    然后你可以在 XAML 中进行绑定:

    <DataTemplate x:Key="AnimalTemplate">
        <Grid x:Name="GridAnimalTemplate" >
        ...
            <Label Content="{Binding AnimalPartLabelBound}" />
    
            <ComboBox ItemsSource="{Binding AnimalStuffBound}" />
        </Grid>
    </DataTemplate>
    

    您可以在 AnimalListboxItem 类中放置任意数量的东西,包括数组、其他列表等。

    【讨论】:

      猜你喜欢
      • 2013-03-23
      • 2019-12-11
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多