使用自定义类的UserControl无法绑定到自定义ItemsSource [英] UserControl using Custom Class can't bind to Custom ItemsSource

查看:72
本文介绍了使用自定义类的UserControl无法绑定到自定义ItemsSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试构建一个自定义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;
		}

		...
	}

推荐答案

在此期间,我遇到了一个例外,说以下内容:

TwoWay或OneWayToSource绑定无法在只读属性

源"ContactRelationRoleList"仅具有一个吸气剂(即ReadOnly).在添加了设置"后,该异常消失了,但ItemsSource仍然没有绑定.
In the meantime I've had an exception saying the following:

A TwoWay or OneWayToSource binding cannot work on the read-only property

The Source 'ContactRelationRoleList' only has a getter (ie ReadOnly). After adding a 'set' as whell this exception disappeard but the ItemsSource still isn't bound.


这篇关于使用自定义类的UserControl无法绑定到自定义ItemsSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆