自定义控件 ContentProperty DataBinding

发布于 2024-10-20 10:45:27 字数 2752 浏览 1 评论 0原文

我在尝试使用作为集合一部分的对象中的依赖属性时遇到问题,在自定义控件内,用“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”只是公开“Columns”和“ItemSource”d 属性的两个 ItemsControl。

这里的问题是,虽然我的 2 个 SmartSearchScope 对象被实例化,但“View”和“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

I'm running in a issue while trying to use dependency properties in objects which are parts of a collection, inside acustom control, collection identified with the "ContentProperty" attribute. Ok, that's quite unclear. Here is sample of my custom control :

Here is my custom control basic definition :

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

Here is the basic definition of a SmartSearchScope object :

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)
}

All that for what ? Being able to pass a collection of SmartSearchScope objects via XAML like so :

<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' and 'CcyPairsConfigBlotter' are just two ItemsControls which expose a 'Columns' and an 'ItemSource' d-property.

The problem here is that althought my 2 SmartSearchScope objects gets instantiated, the databinding on the "View" and "FilterColumns" d-properties is not made and I never go througth the the associated callbacks.

In addition, here is the output error message I got when creating the custom control.

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')

This is obvious that I'm missing something but I can't find what.

I must say that, in a previous version of that control, these 2 problematic d-properties where SmartSearchCc properties and that all worked just fine.

Thanks for your help :)

--bruno

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃温暖了心ぐ 2024-10-27 10:45:27

我在这里遇到了类似的问题: Bindings on child dependency object用户控件不起作用

绑定不起作用的原因是 DependencyObject 没有 DataContext 属性。就我而言,我将它们更改为从 FrameworkElement 继承,这解决了问题。

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

I had a similar problem here: Bindings on child dependency object of usercontrol not working

The reason the binding doesn't work is because DependencyObjects don't have a DataContext property. In my case I changed them to inherit from FrameworkElement which solved the problem.

Although as someone else has mentioned, changing the parent control to an ItemsControl could simplify things.

独留℉清风醉 2024-10-27 10:45:27

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

谢谢大家的建议!

Ok, problem solved, I swithc inheritance of my main custom control from control to ItemsControl and inheritance of my child object to FrameWork element and that's it. no need to further modifications.

Thank you all for your suggestions !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文