我试图在使用“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
嗨,你不能让SmartSearchScope的ItemsControl?我会说这很简单。 – 2011-03-02 10:38:24