【问题标题】:Custom control ContentProperty DataBinding自定义控件 ContentProperty DataBinding
【发布时间】:2011-03-02 10:21:08
【问题描述】:

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

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

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

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

很明显,我遗漏了一些东西,但我找不到什么。

我必须说,在该控件的先前版本中,这 2 个有问题的 d-properties 中的 SmartSearchCc 属性都可以正常工作。

感谢您的帮助:)

--布鲁诺

【问题讨论】:

  • 您好,您不能将 SmartSearchScope 设为 ItemsControl 吗?我会说这会很简单。

标签: wpf data-binding custom-controls contentproperty


【解决方案1】:

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

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

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

【讨论】:

  • 您好 DavidThx 为您解答。确实,这是完全相同的问题。我发现“Freezable”这个东西有点脏,尽管它确实有效。关于将 datacontext 设置为子对象,你什么时候会这样做?
  • 而且,顺便说一句,我不确定从 ItemsControl 继承会如何简化事情。我认为它会复杂得多。不过我会这样看:)
  • 嗨 Bruno - 是的,我没有使用 Freezable 的东西,因为我不需要它的功能 - 我只是想让绑定工作,所以这就是我使用 FrameworkElement 的原因。我在“OnApplyTemplate”中设置了数据上下文。我遍历了子集合并做了 child.DataContext = this.DataContext;
  • 顺便说一句 - 我没有更改集合类型.. 只是从 FrameworkElement 继承而不是 DependencyObject
【解决方案2】:

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

谢谢大家的建议!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    相关资源
    最近更新 更多