C# 为什么DataGrid中没有更新ItemsSource
我的窗口使用C# 为什么DataGrid中没有更新ItemsSource,c#,wpf,C#,Wpf,我的窗口使用MyUserControl并通过MyItemsProperty属性向其传递一个项目集合MyUserControl将项目转换为MyClassBag,并将项目添加到WrappedItems集合(OnMyItemsChanged->CollectionChanged->AddItem),该集合绑定到DataGrid的ItemsSource 问题是ItemsSource没有更新(我在control.WrappedItems.Add(new MyClassBag{…});上设置了制动点,它确实
MyUserControl
并通过MyItemsProperty
属性向其传递一个项目集合MyUserControl
将项目转换为MyClassBag
,并将项目添加到WrappedItems
集合(OnMyItemsChanged->CollectionChanged->AddItem),该集合绑定到DataGrid
的ItemsSource
问题是ItemsSource没有更新(我在control.WrappedItems.Add(new MyClassBag{…});
上设置了制动点,它确实到达了那里)。但是,如果我在
MyUserControl
中放置一个按钮,并通过按钮的单击方法添加项目(WrappedItems.add(newmyclassbag{…});
),则项目资源确实会得到更新。有什么问题
public partial class MyUserControl : UserControl
{
public static readonly DependencyProperty MyItemsProperty =
DependencyProperty.Register("MyItems",
typeof(ObservableCollection<MyClass>),
typeof(MyUserControl),
new PropertyMetadata(
new ObservableCollection<MyClass>(),
new PropertyChangedCallback(OnMyItemsChanged)
)
);
public ObservableCollection<MyClass> MyItems
{
get { return (ObservableCollection<MyItems>)GetValue(MyItemsProperty); }
set { SetValue(MyItemsProperty, value); }
}
private static void OnMyItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
control = d as MyUserControl;
var items = e.NewValue as ObservableCollection<MyClass>;
states.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
AddItem(control, items);
}
private static void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
var items = sender as ObservableCollection<MyClass>;
if (e.Action == NotifyCollectionChangedAction.Add)
{
control.AddNewItem();
}
}
public ObservableCollection<MyClassBag> WrappedItems { get; set; }
public void AddNewItem()
{
WrappedItems.Add(new MyClassBag { ... });
}
private void Button_Click(object sender, RoutedEventArgs e)
{
AddNewItem();
}
}
公共部分类MyUserControl:UserControl
{
公共静态只读从属属性MyItemsProperty=
DependencyProperty.Register(“MyItems”,
类型(可观测采集),
类型(MyUserControl),
新属性元数据(
新的ObservableCollection(),
新物业变更回拨(OnMyItemsChanged)
)
);
公共可观测集合MyItems
{
get{return(ObservableCollection)GetValue(MyItemsProperty);}
set{SetValue(MyItemsProperty,value);}
}
MyItemsChanged上的私有静态无效(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
control=d作为MyUserControl;
var项目=e.作为可观察集合的新值;
states.CollectionChanged+=新通知collectionchangedventhandler(CollectionChanged);
附加项(控制,项目);
}
私有静态void CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
var items=发送方作为可观察到的收款;
if(e.Action==NotifyCollectionChangedAction.Add)
{
control.AddNewItem();
}
}
公共ObservableCollection WrappedItems{get;set;}
public void AddNewItem()
{
Add(新的MyClassBag{…});
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
AddNewItem();
}
}
MyUserControl.XAML
<DataGrid ItemsSource="{Binding WrappedItems ... />
<Button Click="Button_Click" />
不久前我也遇到了类似的问题
请将MyClass
类从Freezable
类继承,这样一切都可以正常工作。请让我知道它是否有帮助。添加控件后,在代码中再次对网格进行数据绑定,看看这是否能解决您的问题。在我再次尝试之前,请给出一个快速提示。@voddy,再次数据绑定网格是什么意思?MyClassBag是否继承INotifyPropertyChanged?不,MyClassBag不继承INotifyPropertyChanged。为什么要这样做?我不会更改我的类包的属性。我只是向collection添加了一些项,我注意到如果我用ItemsControl替换DataGrid,上面的代码就可以工作了。那么为什么它不能与数据网格一起工作呢?
public partial class Window1 : Window
{
public ObservableCollection<MyClass> ItemsForMyUserControl { get; set; }
private void Load()
{
ItemsForMyUserControl.Add(new MyClass(...));
ItemsForMyUserControl.Add(new MyClass(...));
}
}
<uc:MyUserControl MyItems="{Binding ItemsForMyUserControl}" />