2011-03-02 181 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元素,就是这样。无需进一步修改。

谢谢大家的建议!