2011-03-02 179 views
0

我試圖在使用「ContentProperty」屬性標識的集合中的集合的一部分的對象中使用依賴項屬性時遇到問題。好的,這很不明確。這裏是我的自定義控件的樣品:自定義控件ContentProperty數據綁定

這裏是我的自定義控制基本定義:

[ContentProperty("SmarSearchScopes ")] 
public class SmartSearchCc : Control 
{ 
    List<SmartSearchScope> SmarSearchScopes {get;set;} 
    (more code here) 
} 

這裏是一個SmartSearchScope對象的基本定義:

public class SmartSearchScope : DependencyObject 
{ 
    public static readonly DependencyProperty ViewProperty =DependencyProperty.Register("View", typeof (ICollectionView), typeof (SmartSearchScope),new UIPropertyMetadata(null,OnViewChanged)); 

    public static readonly DependencyProperty FilterColumnsProperty =DependencyProperty.Register("FilterColumns", typeof (IEnumerable<ColumnBase>), typeof (SmartSearchScope),new UIPropertyMetadata(null, OnFilterColumnsChanged)); 
    public ICollectionView View 
    { 
     get { return (ICollectionView) GetValue(ViewProperty); } 
     set { SetValue(ViewProperty, value); } 
    } 

    public IEnumerable<ColumnBase> FilterColumns 
    { 
     get { return (IEnumerable<ColumnBase>) GetValue(FilterColumnsProperty); } 
     set { SetValue(FilterColumnsProperty, value); } 
    } 
    (more code here) 
} 

所有這一切是爲了什麼?如果能夠通過XAML來傳遞SmartSearchScope對象的集合,像這樣:

<SmartSearch:SmartSearchCc HorizontalAlignment="Stretch" Grid.Row="0" > 
    <SmartSearch:SmartSearchScope FilterColumns="{Binding ElementName=CcyPairsConfigBlotter, Path=Columns}" View ="{Binding ElementName=CcyPairsConfigBlotter, Path=ItemsSource}"/> 
    <SmartSearch:SmartSearchScope FilterColumns="{Binding ElementName=ClientConfigBlotter, Path=Columns}" View ="{Binding ElementName=ClientConfigBlotter, Path=ItemsSource}"/> 
</SmartSearch:SmartSearchCc> 

「ClientConfigBlotter」和「CcyPairsConfigBlotter」只是兩個ItemsControls這暴露出「列」和「的ItemSource」 d屬性。

這裏的問題是,雖然我的2個SmartSearchScope對象被實例化,但「視圖」和「FilterColumns」d屬性的數據綁定並沒有完成,我從來沒有通過關聯的回調。

另外,這裏是我在創建自定義控件時得到的輸出錯誤消息。

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Columns; DataItem=null; target element is 'SmartSearchScope' (HashCode=56862858); target property is 'FilterColumns' (type 'IEnumerable`1') 
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=ItemsSource; DataItem=null; target element is 'SmartSearchScope' (HashCode=56862858); target property is 'View' (type 'ICollectionView') 

這很明顯,我失去了一些東西,但我找不到什麼。

我必須說,在該控件的以前版本中,這兩個有問題的d屬性,其中SmartSearchCc屬性和所有工作都很好。

感謝您的幫助:)

--bruno

+0

嗨,你不能讓SmartSearchScope的ItemsControl?我會說這很簡單。 – 2011-03-02 10:38:24

回答

1

我有一個類似的問題在這裏:Bindings on child dependency object of usercontrol not working

綁定不工作的原因是因爲DependencyObjects沒有一個DataContext屬性。在我的情況下,我改變了他們從FrameworkElement繼承,這解決了問題。

雖然正如別人所說,將父控件更改爲ItemsControl可以簡化事情。

+0

你好DavidThx爲你的答案。事實上,這是完全相同的問題。我發現「Freezable」的東西有點髒,儘管它確實有效。關於將datacontext設置爲子對象,您何時會這樣做? – Bruno 2011-03-02 13:16:14

+0

而且,順便說一句,我不知道如何繼承ItemsControl將簡化的事情。我認爲它會更復雜。我要去那邊看:) – Bruno 2011-03-02 13:18:47

+0

嗨布魯諾 - 是的,我沒有使用Freezable的東西,因爲我不需要它的功能 - 我只是想讓綁定工作,所以這就是爲什麼我只是使用FrameworkElement。我在'OnApplyTemplate'中設置了datacontext。我迭代了子集合並做了child.DataContext = this。DataContext的; – 2011-03-02 13:44:38

0

好了,問題解決了,我從主控制繼承我的主自定義控件到ItemsControl,並將我的子對象繼承到FrameWork元素,就是這樣。無需進一步修改。

謝謝大家的建議!