簡體   English   中英

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

[英]WPF newbie - setting ItemsSource in XAML doesn't seem to work

我是WPF的新手,正在嘗試弄清所有這些數據綁定的東西。 當我在代碼中執行以下操作時,在運行應用程序時將填充ComboBox:

public NewForm()
{
    InitializeComponent();
    Product.ItemsSource = Products;
}

public List<string> Products
{
    get { return _productsComponents.Keys.ToList(); }
}

但是,當我具有以下內容時,在我的XAML中,運行應用程序時ComboBox中沒有內容:

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

我引用的內容有誤嗎? 本教程很有幫助,但他從未在XAML中始終使用C#設置ItemsSource。

默認情況下,實際上不是綁定到表單本身,而是綁定到分配給DataContext屬性的對象。 這有助於使用視圖模型來管理代碼隱藏文件之外的所有數據。

您可能可以將表單本身分配給構造函數中的DataContext屬性

DataContext = this;

您還可以通過幾種方式中的任何一種來綁定到表單。 這是一個:

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

認為“ Products不需要在這里成為DependencyProperty ,但不要在此引用我的名字,並且只要不更改集合,就不必擔心更新通知。

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}}"

嘗試綁定源=產品

通常,Path是我使用過的Source上的一個附錄,用於需要訪問Source中可用元素的子屬性的情況。

這是一個ListBox的示例,我用存儲在靜態資源中的XDocument中的元素填充了它。 這可能有望解釋綁定源和路徑之間的關系。 Root.Elements是到Source中引用的XDocument仰卧的集合的成員路徑。 不要在您的應用中使用{StaticResource Product}機制,因為您的應用程序xaml中沒有集合,而是當前類中的集合。

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

這個WPF數據綁定備忘單也很方便,我發現它很有幫助: http : //www.nbdtech.com/Blog/archive/2009/02/02/wpf-xaml-data-binding-cheat-sheet.aspx

我建議嘗試限制自己在代碼或XAML中保留綁定設置,而不要混合使用。 在這種情況下,您要在XAML中創建表單時設置ItemsSource。 我不確定為什么嗎?

無論如何,根據您的代碼,您似乎是'_productsComponents'是一個Dictionary。 因此,我將在更好的代碼版本中利用它:

代碼隱藏:

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" ... />

我還將研究: WPF博士的ObservableDictionary 它允許您確保如果Dictionary項更改,則您的組合框(UI)將能夠相應地觀察到該更改(也就是說,如果您從字典中刪除/添加鍵值對,則您的組合框將始終顯示正確的列表)。

暫無
暫無

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

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