2010-07-30 69 views
1

我是新來的WPF,並試圖找出所有這些數據綁定的東西。當我做我的代碼之後,當我運行我的應用我的組合框填充:WPF新手 - 在XAML中設置ItemsSource似乎不起作用

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

我是否引用了一些不正確的東西? This tutorial很有幫助,但他從來沒有在XAML中設置ItemsSource,始終在C#中。

回答

3

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

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

DataContext = this; 

您也可以綁定到任何一種方式的形式。這裏是一個:

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

我不認爲需要Products在這裏一個DependencyProperty,但不可以引用我這句話,只要收集不受改變,你不不需要擔心更新通知。

+0

)<= ComboBox Height =「23」Horizo​​ntalAlignment =「Left」Margin =「138,116,0,0」Name =「Product」VerticalAlignment =「Top」Width =「120」ItemsSource =「{Binding Path = Products,當我運行應用程序時,仍然給我一個空的組合框:( – 2010-07-30 14:31:07

+0

)當我剛纔有''謝謝! – 2010-07-30 14:32:24

+0

@Sarah In第一種情況是,您是否在Window元素中指定了名稱「thisWindow」?'x:Name =「thisWindow」' – Jay 2010-07-30 14:36:14

0

嘗試綁定源=產品

路徑一般爲我是如何用它在源的增編,這是因爲當你需要訪問源可用的元素的子屬性。

下面是一個ListBox的例子,我用靜態資源中存儲的XDocument中的元素填充。這可能有希望解釋綁定源和路徑之間的關係。 Root.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}" /> 

也得心應手這是WPF數據綁定小抄,我發現它非常有用: http://www.nbdtech.com/Blog/archive/2009/02/02/wpf-xaml-data-binding-cheat-sheet.aspx

+0

呃,導致我的應用程序中的組合框包含列表項 'P', 'R', 'O', 'd', 'U',「C ','t','s'。 ( – 2010-07-30 14:28:44

0

我建議試圖限制自己或者保持在代碼或XAML中的綁定設置,而不是混合它們。在你遇到這種情況時,你需要在創建表單和設置XAML時設置ItemsSource。我不知道爲什麼?

無論如何,它似乎是'_productsComponents'是由您的代碼字典。所以我打算用它來我的優勢在一個更好的代碼版本:

後臺代碼:

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。它允許你確保如果字典項目改變,你是組合框(UI)將能夠相應地觀察到這種改變(也就是說,如果你從你的字典中刪除/添加一個keyvaluepair,那麼你將是組合框總是顯示正確的列表)。

+0

哦,如果你是WPF的新手,如果你來自另一種語言,我會考慮拿起一本書......調整WPF的工作方式有點像學習曲線。我在C#2010中選擇了Pro WPF。它表示Pro,但它並不太複雜。 Book + StackOverflow + Dr. WPFs網站=吸取WPF的好方法。 – 2010-07-30 15:07:11

3

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