问题描述
您好,
我的C#/ WPF应用程序中有以下代码。单击Refresh按钮,我在Viewmodel上调用GetData方法并尝试重置observable集合。但是网格中的数据没有得到更新。我在这里做错了什么?CustomObservableCollection类中的Reset方法
逻辑是否正确?
感谢您的帮助。
ViewModel.cs:
$
I've the following code in my C#/WPF application. On clicking of Refresh button, I invoke GetData method on the Viewmodel and try to reset the observable collection. But data in grid is not getting updated. What am I doing wrong here?Is the Reset method logic in CustomObservableCollection class correct?
Thanks for your help.
ViewModel.cs:
private object _myLock = new object();
internal void GetData()
{
var data = ...//Get data from database
lock (_myLock)
{
if (MyCollection != null )
{
MyCollection.Reset(data);
}
else
{
MyCollection = new CustomObservableCollection<Product>(data);
}
}
XAML:
XAML:
<Grid >
<.... ItemsSource="{Binding MyCollection}"
</Grid>
<Button Click="RefreshGridButton_Click" Content="Refresh" />
CustomObservableCollection.cs
CustomObservableCollection.cs
public class CustomObservableCollection<T> : ObservableCollection<T>
{
private object _lock = new object();
public CustomObservableCollection()
: base()
{
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
base.OnCollectionChanged(e);
}
public CustomObservableCollection(IEnumerable<T> collection)
: base(collection)
{
}
public void Reset(IEnumerable<T> range)
{
if (range == this)
{
return;
}
lock (_lock)
{
this.Items.Clear();
foreach (var item in range)
{
this.Items.Add(item);
}
this.OnPropertyChanged(new PropertyChangedEventArgs("Count"));
this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, range, 0));
}
}
}
推荐答案
根据您的描述,您希望在observablecollection更改时刷新数据网格。有两种简单的方法可以做到这一点,你可以考虑一下。
According to your description, you want to refresh datagrid when observablecollection changed. There are two simple way to do this, you can consider it.
一个是实现Inotifypropertychanged接口。
One is implement Inotifypropertychanged interface to.
xaml:
<DataGrid
Name="datagrid1"
Width="500"
Height="300"
AutoGenerateColumns="False"
ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn
Width="50"
Binding="{Binding Id}"
Header="Id" />
<DataGridTextColumn
Width="100"
Binding="{Binding Name}"
Header="Name" />
</DataGrid.Columns>
</DataGrid>
<Button
Name="btnadd"
Width="100"
Height="30"
Margin="0,20,20,0"
Click="btnadd_Click"
Content="additem" />
.cs:
public partial class Window5 : Window,INotifyPropertyChanged
{
private ObservableCollection<Student> _students;
public ObservableCollection<Student> students
{
get { return _students; }
set
{
_students = value;
RaisePropertyChanged("students");
}
}
public Window5()
{
InitializeComponent();
students = new ObservableCollection<Student>()
{
new Student(){Id="A", Name="Cherry"},
new Student() { Id ="B", Name = "Barry" },
new Student() { Id="C",Name="Wendy"},
new Student() { Id="D",Name="Wendy1"},
new Student() { Id="E",Name="Wendy2"},
new Student() { Id="F",Name="Wendy3"}
};
this.DataContext = students;
}
private void btnadd_Click(object sender, RoutedEventArgs e)
{
students.Add(new Student() { Id = "G", Name = "Mattew" });
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
另一种方法是使用collectionviewsource
Another way is to use collectionviewsource
/// Interaction logic for Window5.xaml
/// </summary>
public partial class Window5 : Window
{
public CollectionViewSource ViewSource { get; set; }
//Notice no OnPropertyChange, just a property
public ObservableCollection<Student> TestEntities { get; set; }
public Window5()
{
InitializeComponent();
TestEntities = new ObservableCollection<Student>()
{
new Student(){Id="A", Name="Cherry"},
new Student() { Id ="B", Name = "Barry" },
new Student() { Id="C",Name="Wendy"},
new Student() { Id="D",Name="Wendy1"},
new Student() { Id="E",Name="Wendy2"},
new Student() { Id="F",Name="Wendy3"}
};
this.ViewSource = new CollectionViewSource();
ViewSource.Source = this.TestEntities;
this.DataContext = ViewSource.View;
}
private void btnitem_Click(object sender, RoutedEventArgs e)
{
TestEntities.Add(new Student() { Id = "G", Name = "Mattew" });
ViewSource.View.Refresh();
}
}
最好的问候,
Cherry
这篇关于数据网格更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!