如何获取绑定到 ObservableCollection 的 UserControl 派生类以响应添加?

发布于 2024-11-01 22:54:49 字数 1882 浏览 0 评论 0原文

我的集合看起来很好,因为我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无需解释 2024-11-08 22:54:49

如果您的 ItemSource 是 ObservableCollectionCollectionChanged ,您可能想在 CollectionChanged 上注册一个事件处理程序。 T>

maybe you want to register an event handler on CollectionChanged if your ItemSource is an ObservableCollection< T >

π浅易 2024-11-08 22:54:49

这个问题也许会有帮助。
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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文