我有一个绑定了ObservableCollection的DataGrid。我的数据列表是动态的,因此列表中的项目正在被编辑/添加/删除。
起初,我清除并添加了ObservableCollection,但后来我发现可以刷新ObservableCollection,并且我需要实现CollectionChanged来实现这一点,但我不确定如何做到这一点,如果有人能给出一些指针或示例代码,那就太好了。
这是我之前拥有的内容。
private List<OrderList> m_OrderListData = new List<OrderList>();
public List<OrderList> OrderListData
{
get => m_OrderListData;
private set => Set(ref m_OrderListData, value);
}
private ObservableCollection<OrderList> m_OrderListDataCollection;
public ObservableCollection<OrderList> OrderListDataCollection
{
get => m_OrderListDataCollection;
private set => Set(ref m_OrderListDataCollection, value);
}
...
...
m_OrderListDataCollection = new ObservableCollection<OrderList>(m_OrderListData as List<OrderList>);
...
...
foreach (OrderListViewModel Order in OrderList)
{
OrderListData.Add(new OrderList(Order.Description, Order.OrderId));
}
这是我之前拥有的内容。
OrderListData.Clear();
foreach (OrderListViewModel Order in OrderList)
{
OrderListData.Add(new OrderList(Order.Description, Order.OrderId));
}
m_OrderListDataCollection.Clear();
OrderListData.ToList().ForEach(m_OrderListDataCollection.Add);
XAML
<Label Content="OrderList"/>
<DataGrid Name="dgOrderList"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=OrderListDataCollection}"
IsReadOnly="True"
SelectionMode="Single"
SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn Width="Auto" Header="ID" Binding="{Binding OrderId}"/>
<DataGridTextColumn Width="*" Header="Description" Binding="{Binding OrderDescription}"/>
</DataGrid.Columns>
</DataGrid>
编辑: OrderList 类
public class OrderList : INotifyPropertyChanged
{
private string m_OrderDescription;
private string m_OrderId;
public string OrderDescription
{
get => m_OrderDescription;
set => Set(ref m_OrderDescription, value);
}
public string OrderId
{
get => m_OrderId;
set => Set(ref m_OrderId, value);
}
#region Constructor
public OrderList()
{
}
public OrderList(string description, string id)
{
m_OrderDescription = description;
m_OrderId = id;
}
#endregion
#region INotifyPropertyChanged
/// <summary>Updates the property and raises the changed event, but only if the new value does not equal the old value. </summary>
/// <param name="PropName">The property name as lambda. </param>
/// <param name="OldVal">A reference to the backing field of the property. </param>
/// <param name="NewVal">The new value. </param>
/// <returns>True if the property has changed. </returns>
public bool Set<U>(ref U OldVal, U NewVal, [CallerMemberName] string PropName = null)
{
VerifyPropertyName(PropName);
return Set(PropName, ref OldVal, NewVal);
}
/// <summary>Updates the property and raises the changed event, but only if the new value does not equal the old value. </summary>
/// <param name="PropName">The property name as lambda. </param>
/// <param name="OldVal">A reference to the backing field of the property. </param>
/// <param name="NewVal">The new value. </param>
/// <returns>True if the property has changed. </returns>
public virtual bool Set<U>(string PropName, ref U OldVal, U NewVal)
{
if (Equals(OldVal, NewVal))
{
return false;
}
OldVal = NewVal;
RaisePropertyChanged(new PropertyChangedEventArgs(PropName));
return true;
}
/// <summary>Raises the property changed event. </summary>
/// <param name="e">The arguments. </param>
protected virtual void RaisePropertyChanged(PropertyChangedEventArgs e)
{
var Copy = PropertyChanged;
Copy?.Invoke(this, e);
}
/// <summary>
/// Raised when a property on this object has a new value.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Warns the developer if this object does not have
/// a public property with the specified name. This
/// method does not exist in a Release build.
/// </summary>
[Conditional("DEBUG")]
[DebuggerStepThrough]
protected virtual void VerifyPropertyName(string PropertyName)
{
// Verify that the property name matches a real,
// public, instance property on this object.
if (TypeDescriptor.GetProperties(this)[PropertyName] == null)
{
string ErrorMsg = "Invalid Property Name: " + PropertyName + "!";
if (ThrowOnInvalidPropertyName)
{
throw new Exception(ErrorMsg);
}
Debug.Fail(ErrorMsg);
}
}
/// <summary>
/// Returns whether an exception is thrown, or if a Debug.Fail() is used
/// when an invalid property name is passed to the VerifyPropertyName method.
/// The default value is false, but subclasses used by unit tests might
/// override this property's getter to return true.
/// </summary>
protected virtual bool ThrowOnInvalidPropertyName { get; } = true;
ObservableCollection
绑定到DataGrid
的Source
,那么它应该按预期工作。每当添加新项或删除项时,视图将收到通知以更新其数据。要跟踪实际更改,您不需要实现列表的CollectionChanged事件,但必须使列表中的实际对象可观察。一旦对象是可观察的,并且属性发送PropertyChanged
通知,可观察集合将捕获此通知。 - Oceans