Silverlight ObservableCollection 依赖属性在设计模式下不会更新

发布于 2024-10-15 14:28:20 字数 1557 浏览 7 评论 0原文

我在控件中使用 ObservableCollection<> 类型的依赖属性。我想在编辑 XAML 代码时自动更改控件中的内容显示。我写了这样的代码:

public class RowInfoCollection : ObservableCollection<RowInfo>
{
}

//... here my control class
    public static readonly DependencyProperty RowDataProperty = 
        DependencyProperty.Register("RowData", typeof(RowInfoCollection), typeof(VisiGrid), 
        new PropertyMetadata(new RowInfoCollection(), RowDataChangedCallback));

    public RowInfoCollection RowData
    {
        get
        {
            return (RowInfoCollection)base.GetValue(RowDataProperty);
        }
        set
        {
            base.SetValue(RowDataProperty, value);
        }
    }

    private static void RowDataChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        VisiGrid vg = d as VisiGrid;
        vg.rowData = (RowInfoCollection)e.NewValue;
        vg.rowData.CollectionChanged += vg.rowData_CollectionChanged;
        // Some application specific logic skipped
    }

    void rowData_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        rowData = RowData;
        switch (e.Action)
        {
            // Skipped
        }
    }

在运行时,一切正常。但是当我更改VS2010设计器中的属性值时,出现了一些问题。当我完全删除值或插入它而不是空的 VS 设计器时,成功刷新了我的控件的显示。但是,当我一次添加或删除一个集合项目时,没有任何变化。当我构建项目时,显示会更新(实际上,设计器会重新加载)。

当我查看调试器时,我发现在设计模式下,当集合被完全删除或插入时,它成功调用我的回调函数,但在部分更改时不会触发 CollectionChanges 事件。

我应该如何强制 VS 设计器跟踪集合更改和触发事件?

谢谢。

I use a dependency property of type ObservableCollection<> in my control. I want to automatically change content display in my control while editing XAML code. I write such a code:

public class RowInfoCollection : ObservableCollection<RowInfo>
{
}

//... here my control class
    public static readonly DependencyProperty RowDataProperty = 
        DependencyProperty.Register("RowData", typeof(RowInfoCollection), typeof(VisiGrid), 
        new PropertyMetadata(new RowInfoCollection(), RowDataChangedCallback));

    public RowInfoCollection RowData
    {
        get
        {
            return (RowInfoCollection)base.GetValue(RowDataProperty);
        }
        set
        {
            base.SetValue(RowDataProperty, value);
        }
    }

    private static void RowDataChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        VisiGrid vg = d as VisiGrid;
        vg.rowData = (RowInfoCollection)e.NewValue;
        vg.rowData.CollectionChanged += vg.rowData_CollectionChanged;
        // Some application specific logic skipped
    }

    void rowData_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        rowData = RowData;
        switch (e.Action)
        {
            // Skipped
        }
    }

When in run time, everything works ok. But when I change the property value in VS2010 designer there are some problems. When I completely remove value or insert it instead of empty VS' designer successfully refreshes display of my control. But when I add or remove collection items one at a time there's no change. When I build project the display updates (in fact, the designer reloads).

When I looked under debugger I saw that when in design mode it successfully calls my callback function when the collection is totally removed or inserted but does not fire CollectionChanges event when it is changed partially.

How should I force VS designer to track collection changes and fire events?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文