首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义控件ContentProperty DataBinding

自定义控件ContentProperty DataBinding
EN

Stack Overflow用户
提问于 2011-03-02 18:21:08
回答 2查看 2.3K关注 0票数 1

当我试图在对象中使用依赖属性时,我遇到了一个问题,这些对象是一个集合的一部分,在一个自定义控件中,集合由"ContentProperty“属性标识。好吧,这是很不清楚的。下面是我的自定义控件示例:

下面是我的自定义控件的基本定义:

代码语言:javascript
复制
[ContentProperty("SmarSearchScopes ")]
public class SmartSearchCc : Control
{
    List<SmartSearchScope> SmarSearchScopes {get;set;}
    (more code here)
}

下面是SmartSearchScope对象的基本定义:

代码语言:javascript
复制
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对象,如下所示:

代码语言:javascript
复制
<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属性。

这里的问题是,尽管我的两个SmartSearchScope对象被实例化了,但"View“和"FilterColumns”d-properties上的数据绑定并没有进行,我也从来没有经历过相关的回调。

此外,这是我在创建自定义控件时得到的输出错误消息。

代码语言:javascript
复制
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属性和那个都工作得很好。

感谢您的帮助:)

--布鲁诺

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-02 19:37:56

我在这里遇到了类似的问题:Bindings on child dependency object of usercontrol not working

绑定不起作用的原因是因为DependencyObjects没有DataContext属性。在我的示例中,我将它们更改为从FrameworkElement继承,这解决了问题。

尽管如其他人所提到的,将父控件更改为ItemsControl可以简化事情。

票数 1
EN

Stack Overflow用户

发布于 2011-03-02 23:05:01

好了,问题解决了,我将主自定义控件的继承从控件切换到ItemsControl,将子对象的继承切换到FrameWork元素,仅此而已。不需要进一步修改。

感谢大家的建议!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5166045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档