2014-04-24 117 views
0

所以,我試圖執行一些數據綁定到我有的自定義組件,但我似乎無法找到任何有關如何這樣做的好信息。我想要做的僅僅是在有Bindning屬性主窗口中的自定義組件...WPF自定義組件Datagrid綁定

<local:MultiColumnComboBox ItemsSource="{Binding Customers}" x:Name="NewCombo"></local:MultiColumnComboBox> 

然後在自定義組件...

<DataGrid ItemsSource="{Binding ItemsSource}" Name="dataGrid"></DataGrid> 

如果有誰知道怎麼樣要做到這一點,一些指導,將不勝感激:)

編輯

public static readonly DependencyProperty ItemsSourceProperty = 
DependencyProperty.Register("ItemsSource", typeof(IList<Customer>), typeof(MultiColumnComboBox)); 
public MultiColumnComboBox() 
{ 
    InitializeComponent(); 
} 
//Items Source Binding 
public IList<Customer> ItemsSource 
{ 
    get 
    { 
     return (IList<Customer>)GetValue(ItemsSourceProperty); 
    } 
    set 
    { 
     System.Console.WriteLine("Binding"); 
     System.Console.WriteLine(value); 
     SetValue(ItemsSourceProperty, value); 
    } 
} 
+0

您應該使用DependencyProperty。這是我見過的最完整,最易於理解的鏈接。 http://www.codeproject.com/Articles/224230/Exploring-the-use-of-Dependency-Properties-in-User – FodderZone

+0

謝謝,我已經看到了這個,但在該項目**公共IEnumerable ItemsSource ** (我需要的部分)給出錯誤並要求您添加1個類型參數。 – Kapow36

+0

另外,如果我使用推薦的** System.Collections.IEnumerable **它會給出錯誤**不能在'MultiColumnComboBox'類型的'ItemsSource'屬性上設置'Binding'。 '綁定'只能在DependencyObject的DependencyProperty上設置。** – Kapow36

回答

0

我已經想通了!

所有你需要做的就是用從控制DataContext屬性,以便在您的主網頁..

<local:custom control DataContext="{Binding Something}" /> 

而且在您的自定義控制,我綁定...

ItemsSource="{Binding DataContext, ElementName=UOMControl}" 

和就是這樣。

0

當我將錯誤的ownerType傳遞給DependencyProperty.Register方法時,我得到了Binding錯誤...請檢查以確保您使用typeof(MultiColumnComboBox),如下所示。

/// <summary> 
/// Interaction logic for MultiColumnComboBox.xaml 
/// </summary> 
public partial class MultiColumnComboBox : UserControl 
{ 
    /// <summary> 
    /// This creates the dependency property for the collection to display. 
    /// </summary> 
    public static readonly DependencyProperty ItemsSourceProperty = 
     DependencyProperty.Register("ItemsSource", typeof(IList<Customer>), typeof(MultiColumnComboBox)); 

    /// <summary> 
    /// This property gets you to the collection that's being displayed. 
    /// </summary> 
    public IList<Customer> ItemsSource 
    { 
     get { return (IList<Customer>)GetValue(ItemsSourceProperty); } 
     set { SetValue(ItemsSourceProperty, value); } 
    } 

    public MultiColumnComboBox() 
    { 
     InitializeComponent(); 
    } 
} 

另外,請確保在UserControl中設置了Binding ElementName,以便它具有正確的DataContext。

<UserControl x:Class="DependencyPropertyExample.MultiColumnComboBox" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      x:Name="userControl1"> 
    <Grid> 
     <DataGrid ItemsSource="{Binding Path=ItemsSource, ElementName=userControl1}" /> 
    </Grid> 
</UserControl> 
+0

有幾件事(抱歉已經這麼久了,我已經離開電腦一段時間了),到目前爲止我無法讓這個工作。它不識別綁定。我知道示例數據庫設置正確,因爲我有一個datagrid,我可以將其綁定到工作正常。此外,上述代碼中的IList聲明包括Customer類型,但是該控件並不總是將Customer作爲項目源,所以是的。我已經得到了迄今爲​​止的代碼。我沒有包括非必需品... – Kapow36

+0

我已將代碼添加到原始帖子btw – Kapow36