问题描述
我正在尝试构建一个自定义UserControl,该用户控件包含2个其他控件(对于我遇到的问题并不重要).在下面,您将找到我创建的两个类(用于承载"Groups"属性的UserControl和用于承载"ItemsSource"属性的OverviewControlGroup类).在此之下,您将找到XAML,在该XAML中,我尝试将Groups中的Group的ItemsSource绑定到DataContext的属性.我已经为此苦苦挣扎了两天,但仍然没有找到解决问题的方法.我已经阅读了有关MSDN上的绑定和自定义类的所有文章,但还没有发现任何可能出问题的线索.我还使用标准ListView测试了我们的属性(称为"ContactRelationRoleList"的源),该属性可以按预期工作(意味着数据存在).
Hi,
I'm trying to build a Custom UserControl that hosts 2 other controls (which are not important for the problem I have). Below you'll find 2 of the classes I've created (the UserControl, hosting the 'Groups' property and the OverviewControlGroup Class hosting the 'ItemsSource' property). Below that you'll find the XAML in which I try to bind the ItemsSource of a Group in the Groups to property of the DataContext. I've been struggling with this for 2 days now and still haven't found a solution for the problem. I've read all there is to read about binding and custom classes on MSDN but haven't found any clues to what could be wrong. I've also tested our property (the source called 'ContactRelationRoleList') with the standard ListView and that works as expected (meaning, the data is present).
public partial class KLIBOverviewControl : KLIBUserControl
{
public List<KLIBOverviewControlGroup> Groups
{
get { return (List<KLIBOverviewControlGroup>)GetValue(GroupsProperty); }
set { SetValue(GroupsProperty, value); }
}
public static readonly DependencyProperty GroupsProperty =
DependencyProperty.Register("Groups", typeof(List<KLIBOverviewControlGroup>), typeof(KLIBOverviewControl),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public KLIBOverviewControl()
{
// We need to initialize this list per instance. We can't therefore use the default value of the UIPropertyMetadata() class.
Groups = new List<KLIBOverviewControlGroup>();
InitializeComponent();
}
...
}
public class KLIBOverviewControlGroup : FrameworkElement
{
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(KLIBOverviewControlGroup),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnItemsSourceChanged));
private static void OnItemsSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
KLIBOverviewControlGroup group = (KLIBOverviewControlGroup)sender;
}
...
}
推荐答案
这篇关于使用自定义类的UserControl无法绑定到自定义ItemsSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!