Wpf 自定义控件ContentProperty数据绑定
我在尝试在对象中使用依赖属性时遇到了一个问题,这些对象是集合的一部分,在acustom控件中,集合由“ContentProperty”属性标识。好吧,这还不清楚。以下是我的自定义控件示例: 以下是我的自定义控件的基本定义:Wpf 自定义控件ContentProperty数据绑定,wpf,data-binding,custom-controls,contentproperty,Wpf,Data Binding,Custom Controls,Contentproperty,我在尝试在对象中使用依赖属性时遇到了一个问题,这些对象是集合的一部分,在acustom控件中,集合由“ContentProperty”属性标识。好吧,这还不清楚。以下是我的自定义控件示例: 以下是我的自定义控件的基本定义: [ContentProperty("SmarSearchScopes ")] public class SmartSearchCc : Control { List<SmartSearchScope> SmarSearchScopes {get;set;}
[ContentProperty("SmarSearchScopes ")]
public class SmartSearchCc : Control
{
List<SmartSearchScope> SmarSearchScopes {get;set;}
(more code here)
}
很明显,我遗漏了什么,但我找不到什么
我必须说,在该控件的前一个版本中,这两个有问题的d属性,其中SmartSearchCc属性和其他属性都工作得很好
谢谢你的帮助:)
--布鲁诺我在这里遇到了类似的问题: 绑定不起作用的原因是DependencyObjects没有DataContext属性。在我的例子中,我将它们改为从FrameworkElement继承,从而解决了这个问题
尽管正如其他人提到的,将父控件更改为ItemsControl可以简化事情 我在这里遇到了类似的问题: 绑定不起作用的原因是DependencyObjects没有DataContext属性。在我的例子中,我将它们改为从FrameworkElement继承,从而解决了这个问题
尽管正如其他人提到的,将父控件更改为ItemsControl可以简化事情 好的,问题解决了,我将主自定义控件的继承从控件切换到ItemsControl,并将子对象的继承切换到FrameWork元素,就这样。无需进一步修改
谢谢大家的建议 好的,问题解决了,我将主自定义控件的继承从控件切换到ItemsControl,并将子对象的继承切换到FrameWork元素,就这样。无需进一步修改
谢谢大家的建议 您好,您不能将SmartSearchScope设置为ItemsControl吗?我想说这会让事情变得复杂。嗨,你能不能让SmartSearchScope成为一个ItemsControl?我想说这会让事情变得复杂很多。你好,DavidThx,谢谢你的回答。事实上,这正是同样的问题。我发现“可自由化”的东西有点脏,尽管它确实有效。关于将datacontext设置为子对象,您什么时候会这样做?顺便说一句,我不确定从ItemsControl继承将如何简化事情。我以为会复杂得多。不过我会这样看:)嗨,布鲁诺-是的,我没有使用Freezable,因为我不需要它的功能-我只是想让绑定正常工作,所以我才使用FrameworkElement。我在“OnApplyTemplate”中设置了datacontext。我迭代了子集合,并执行child.DataContext=this.DataContext;顺便说一句,我没有改变收集类型。。只是字面上从FrameworkElement继承,而不是DependencyObjectHello DavidThx来回答您的问题。事实上,这正是同样的问题。我发现“可自由化”的东西有点脏,尽管它确实有效。关于将datacontext设置为子对象,您什么时候会这样做?顺便说一句,我不确定从ItemsControl继承将如何简化事情。我以为会复杂得多。不过我会这样看:)嗨,布鲁诺-是的,我没有使用Freezable,因为我不需要它的功能-我只是想让绑定正常工作,所以我才使用FrameworkElement。我在“OnApplyTemplate”中设置了datacontext。我迭代了子集合,并执行child.DataContext=this.DataContext;顺便说一句,我没有改变收集类型。。只是从FrameworkElement继承,而不是从DependencyObject继承
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)
}
<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>
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')