绑定集合子项的值
看这个示例:
我创建了一个自定义控件,其中包含一个集合作为依赖属性,并使用从绑定获取值的子项填充集合的项。如果我创建具有固定值的子项,则一切正常,如果我绑定它们的值,则会出现绑定错误。
这是具有只读依赖属性的用户控件:
public partial class UserControl1 : UserControl
{
private static DependencyPropertyKey TextsPropertyKey = DependencyProperty.RegisterReadOnly("Texts", typeof(ItemCollection), typeof(UserControl1), new FrameworkPropertyMetadata(new ItemCollection()));
public static DependencyProperty TextsProperty = TextsPropertyKey.DependencyProperty;
public ItemCollection Texts
{
get
{
return (ItemCollection)GetValue(TextsProperty);
}
set
{
SetValue(TextsProperty, value);
}
}
public UserControl1()
{
ItemCollection texts = new ItemCollection();
SetValue(TextsPropertyKey, texts);
InitializeComponent();
}
}
这是 Window XAML:
<Window x:Class="ControlList.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ControlList="clr-namespace:ControlList"
Title="Window1" Height="300" Width="300">
<Grid>
<ControlList:UserControl1>
<ControlList:UserControl1.Texts>
<ControlList:ItemOfTheList Text="{Binding Text1}"></ControlList:ItemOfTheList>
<ControlList:ItemOfTheList Text="{Binding Text2}"></ControlList:ItemOfTheList>
</ControlList:UserControl1.Texts>
</ControlList:UserControl1>
</Grid>
</Window>
ItemOfTheList 类只是一个具有字符串依赖属性的对象:
public class ItemOfTheList : DependencyObject
{
public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ItemOfTheList));
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
public override string ToString()
{
return this.Text;
}
}
并且项目集合只是一个非泛型 FreezableCollection:
public class ItemCollection : FreezableCollection<ItemOfTheList>
{
}
通过这种方式,我收到以下错误:
System.Windows.Data 错误:2:找不到管理 FrameworkElement 或 FrameworkContentElement 作为目标元素。 绑定表达式:Path=Text1;数据项=空;目标元素是 'ItemOfTheList'(哈希码=52697953);目标属性是“文本”(类型 'String') System.Windows.Data 错误:2:找不到管理 目标元素的 FrameworkElement 或 FrameworkContentElement。 绑定表达式:Path=Text2;数据项=空;目标元素是 'ItemOfTheList'(哈希码=22597652);目标属性是“文本”(类型 '字符串')
如果我将 ItemOfTheList 更改为 FrameworkElement,我总是在输出窗口中得到 DataContext 为空的信息。如何继承 ItemOfTheList
对象中 UserControl
的数据上下文?
Look at this example:
I've created a custom control with a collection as a dependency property and filled the items of the collection with subitems getting the values from a binding. If I create the subitems with fixed values everything works, if I bind their values I get binding errors.
This is the user control with the readonly dependency property:
public partial class UserControl1 : UserControl
{
private static DependencyPropertyKey TextsPropertyKey = DependencyProperty.RegisterReadOnly("Texts", typeof(ItemCollection), typeof(UserControl1), new FrameworkPropertyMetadata(new ItemCollection()));
public static DependencyProperty TextsProperty = TextsPropertyKey.DependencyProperty;
public ItemCollection Texts
{
get
{
return (ItemCollection)GetValue(TextsProperty);
}
set
{
SetValue(TextsProperty, value);
}
}
public UserControl1()
{
ItemCollection texts = new ItemCollection();
SetValue(TextsPropertyKey, texts);
InitializeComponent();
}
}
This is the Window XAML:
<Window x:Class="ControlList.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ControlList="clr-namespace:ControlList"
Title="Window1" Height="300" Width="300">
<Grid>
<ControlList:UserControl1>
<ControlList:UserControl1.Texts>
<ControlList:ItemOfTheList Text="{Binding Text1}"></ControlList:ItemOfTheList>
<ControlList:ItemOfTheList Text="{Binding Text2}"></ControlList:ItemOfTheList>
</ControlList:UserControl1.Texts>
</ControlList:UserControl1>
</Grid>
</Window>
The ItemOfTheList class is just an object with a string Dependency property:
public class ItemOfTheList : DependencyObject
{
public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ItemOfTheList));
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
public override string ToString()
{
return this.Text;
}
}
And the item collection is just a non generic FreezableCollection:
public class ItemCollection : FreezableCollection<ItemOfTheList>
{
}
In this way I get the following errors:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement
or FrameworkContentElement for target element.
BindingExpression:Path=Text1; DataItem=null; target element is
'ItemOfTheList' (HashCode=52697953); target property is 'Text' (type
'String') System.Windows.Data Error: 2 : Cannot find governing
FrameworkElement or FrameworkContentElement for target element.
BindingExpression:Path=Text2; DataItem=null; target element is
'ItemOfTheList' (HashCode=22597652); target property is 'Text' (type
'String')
If I change ItemOfTheList to a FrameworkElement I always get at the output window that the DataContext is null. How can I inherit the datacontext of the UserControl
in the ItemOfTheList
objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的子项不是逻辑树的一部分,因此无法参与绑定。请参阅我的博客文章此处,了解有关如何纠正此问题的详细信息。
Your child items are not part of the logical tree so cannot participate in bindings. Please see my blog post here for details on how to rectify this.