代码之家  ›  专栏  ›  技术社区  ›  Sarah Vessels

WPF新手在XAML中设置ItemsSource似乎不起作用

  •  1
  • Sarah Vessels  · 技术社区  · 15 年前

    public NewForm()
    {
        InitializeComponent();
        Product.ItemsSource = Products;
    }
    
    public List<string> Products
    {
        get { return _productsComponents.Keys.ToList(); }
    }
    

    <ComboBox Height="23" HorizontalAlignment="Left" Margin="138,116,0,0"
              Name="Product" VerticalAlignment="Top" Width="120"
              ItemsSource="{Binding Path=Products}"/>
    

    我引用的东西不对吗? This tutorial 很有帮助,但他从不在XAML中设置ItemsSource,总是在C#。

    4 回复  |  直到 15 年前
        1
  •  3
  •   Jay    15 年前

    默认情况下,您实际上并没有绑定到窗体本身,而是绑定到指定给 DataContext

    您可以将窗体本身指定给 数据上下文

    DataContext = this;
    

    您还可以通过以下几种方式之一绑定到窗体。这里有一个:

    <Window x:Name="thisWindow" …
        <ComboBox ItemsSource="{Binding Path=Products, ElementName=thisWindow}"…
    

    我不 认为 Products DependencyProperty 这里,但不要引用我的话,只要集合不受更改,您就不需要担心更新通知。

        2
  •  3
  •   Gijs    12 年前

    DataContext="{Binding RelativeSource={RelativeSource Self}}"

    <Window x:Class="ListViewTest.Test2.ListViewTest"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       DataContext="{Binding RelativeSource={RelativeSource Self}}"
    
        3
  •  0
  •   Jimmy Hoffa    15 年前

    下面是一个列表框的示例,我用静态资源中存储的XDocument中的元素填充了它。这也许可以解释绑定源和路径之间的关系。Elements是指向源中引用的XDocument的集合的成员路径。不要在自己的类中使用{StaticResource Product}机制,因为app xaml中没有集合,而是当前类。

    <ListBox Height="660" Name="ResponsesListBox" Width="240"
                          MouseDoubleClick="ResponsesListBox_MouseDoubleClick"
                          MouseLeftButtonDown="WindowMoveHandler"
                          ItemsSource="{Binding Source={StaticResource ResponsesXDocument}, Path=Root.Elements}"
                          ItemTemplate="{StaticResource ListBoxDataTemplate}" />
    

    http://www.nbdtech.com/Blog/archive/2009/02/02/wpf-xaml-data-binding-cheat-sheet.aspx

        4
  •  0
  •   myermian    15 年前

    我建议尽量限制自己将绑定设置保存在代码或XAML中,而不是混合使用它们。在这种情况下,您需要在表单创建时和XAML中设置ItemsSource。我不知道为什么?

    代码隐藏:

    public partial class NewForm : Window
    {
        private Dictionary<String, String> _productsComponents;
        public Products
        {
            get { return _productsComponents; }
            set { _productsComponents= value; }
        }
    
        public NewForm()
        {
            Products = new Dictionary<String, String>();
            //Do you're dictionary loading...
    
            InitializeComponent();
    
            DataContext = this;
    
            ProductCmbBox.ItemsSource = Products;
            ProductCmbBox.SelectedValuePath = "Key";
            ProductCmbBox.DisplayMemberPath = "Value"; //or "Key" if you want...
        }
    }
    
    <ComboBox x:Name="ProductCmbBox" ... />
    

    Dr. WPF's ObservableDictionary . 它允许您确保如果字典项发生更改,您的combobox(UI)将能够相应地观察到该更改(即,如果您从您的字典中删除/添加keyvaluepair,您的combobox将始终显示正确的列表)。

    推荐文章