Silverlight ObservableCollection 依赖属性在设计模式下不会更新
我在控件中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论