如何获取绑定到 ObservableCollection 的 UserControl 派生类以响应添加?
我的集合看起来很好,因为我可以使用 ListView 并且它可以正确响应添加,但是当我将列表视图嵌套到 UserControl 中时,它却不能。我已经提供了相关代码。
我以这种方式创建了一个 UserControl 派生类:
public partial class MyCtrl: UserControl
{
#region Static Properties
public static readonly DependencyProperty ItemsSourceProperty =
ItemsControl.ItemsSourceProperty.AddOwner(
typeof(MyCtrl),
new PropertyMetadata(MyCtrl.ItemsSourcePropertyChangedCallback));
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static void ItemsSourcePropertyChangedCallback(
DependencyObject controlInstance,
DependencyPropertyChangedEventArgs e)
{
MyCtrl myInstance=(MyCtrl)controlInstance;
myInstance.nestedList.ItemsSource=e.NewValue as IEnumerable;
}
}
使用如下所示的 XAML:
<UserControl x:Class="MyCtrl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ListView Name="nestedList" />
</Grid>
</UserControl>
我使用的 XAML 如下所示:
<MyCtrl x:Name="myInstance" ItemsSource="{Binding Path=MyCollection}" />
集合的定义如下:
public static readonly DependencyProperty MyCollectionProperty =
DependencyProperty.Register("MyCollection",
typeof(ObservableCollection<MyObject>),
typeof(ConsumingObject),
new PropertyMetadata(new ObservableCollection<MyObject>());
public ObservableCollection<MyObject> MyCollection
{
get { return (ObservableCollection<MyObject>)this.GetValue(MyCollectionProperty); }
set { this.SetValue(MyCollectionProperty, value); }
}
My collection seems fine as I am able to use a ListView and it responds correctly to adds, however when I nest a listview into a UserControl it doesn't. I've provided the pertinent code.
I've created a UserControl derived class in this fashion:
public partial class MyCtrl: UserControl
{
#region Static Properties
public static readonly DependencyProperty ItemsSourceProperty =
ItemsControl.ItemsSourceProperty.AddOwner(
typeof(MyCtrl),
new PropertyMetadata(MyCtrl.ItemsSourcePropertyChangedCallback));
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static void ItemsSourcePropertyChangedCallback(
DependencyObject controlInstance,
DependencyPropertyChangedEventArgs e)
{
MyCtrl myInstance=(MyCtrl)controlInstance;
myInstance.nestedList.ItemsSource=e.NewValue as IEnumerable;
}
}
With the XAML like this:
<UserControl x:Class="MyCtrl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ListView Name="nestedList" />
</Grid>
</UserControl>
My consuming XAML looks like this:
<MyCtrl x:Name="myInstance" ItemsSource="{Binding Path=MyCollection}" />
Where the collection is defined like this:
public static readonly DependencyProperty MyCollectionProperty =
DependencyProperty.Register("MyCollection",
typeof(ObservableCollection<MyObject>),
typeof(ConsumingObject),
new PropertyMetadata(new ObservableCollection<MyObject>());
public ObservableCollection<MyObject> MyCollection
{
get { return (ObservableCollection<MyObject>)this.GetValue(MyCollectionProperty); }
set { this.SetValue(MyCollectionProperty, value); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的
ItemSource
是 ObservableCollectionCollectionChanged
,您可能想在CollectionChanged
上注册一个事件处理程序。 T>maybe you want to register an event handler on
CollectionChanged
if yourItemSource
is an ObservableCollection< T >这个问题也许会有帮助。
myInstance 的 DataContext 是什么?
代码能转到这一行吗?
myInstance.nestedList.ItemsSource=e.NewValue 作为 IEnumerable;
如果是,那么nestedList是否为空?
This questions maybe can help.
What is the DataContext of myInstance?
Was the code able to go to this line?
myInstance.nestedList.ItemsSource=e.NewValue as IEnumerable;
If yes then is nestedList null or not?