我在 XamDataGrid 中有一个可编辑的字段,当此字段中的文本发生更改时,我想更新绑定到该字段的属性。我发现这可以通过使用DataItemUpdateTrigger来实现,我如何让它更新属性?
另外,有没有办法在编辑字段时在数据上下文上调用方法?
<igDP:XamDataGrid DataSource="{Binding MapDesignNamesDetailViewModelCollection}" AutoFit="True" GroupByAreaLocation="None">
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AutoGenerateFields="False"/>
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:FieldLayout.Fields>
<igDP:Field Name="MapDesignName" Label="Map Design Name" AllowEdit="False"/>
<igDP:Field Name="MapDesignDisplayName" Label="Display Name">
<igDP:Field.Settings>
<igDP:FieldSettings DataItemUpdateTrigger="OnCellValueChange" />
</igDP:Field.Settings>
</igDP:Field>
</igDP:FieldLayout.Fields>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
问题似乎是二传手没有被击中,这是我模型的代码:
public class MapDesign : BindableBase
{
public MapDesign(string mapDesignName, string mapDesignDisplayName)
{
MapDesignName = mapDesignName;
MapDesignDisplayName = mapDesignDisplayName;
}
private string _mapDesignName;
public string MapDesignName
{
get { return _mapDesignName; }
set
{
SetProperty(ref _mapDesignName, value);
}
}
private string _mapDesignDisplayName;
public string MapDesignDisplayName
{
get { return _mapDesignDisplayName; }
set
{
SetProperty(ref _mapDesignDisplayName, value);
}
}
}
当数据收集项实现 INotifyPropertyChanged 接口时,绑定到字段的属性将自动更新,如下例所示。但是,如果需要在用户修改单元格值时获取事件,则可以使用 CellChanged 事件处理程序。希望有帮助。
XAML:
<igDP:XamDataGrid DataSource="{Binding NamesCollection}" AutoFit="True" GroupByAreaLocation="None" CellChanged="XamDataGrid1_CellChanged">
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AutoGenerateFields="False"/>
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:FieldLayout.Fields>
<igDP:Field Name="FirstName" Label="First Name" AllowEdit="False"/>
<igDP:Field Name="LastName" Label="Last Name">
<igDP:Field.Settings>
<igDP:FieldSettings DataItemUpdateTrigger="OnCellValueChange" />
</igDP:Field.Settings>
</igDP:Field>
</igDP:FieldLayout.Fields>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
代码隐藏:
using System.Windows;
namespace XamDataGridUpdateTrigger
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
NamesCollection.Add(new NamesItem("Sport", "Chief"));
InitializeComponent();
}
public NamesModel NamesCollection
{
get { return (NamesModel)GetValue(NamesProperty); }
set { SetValue(NamesProperty, value); }
}
// Using a DependencyProperty as the backing store for Names. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NamesProperty =
DependencyProperty.Register("NamesCollection", typeof(NamesModel), typeof(MainWindow), new PropertyMetadata(new NamesModel()));
private void XamDataGrid1_CellChanged(object sender, Infragistics.Windows.DataPresenter.Events.CellChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"{e.Cell}");
}
}
}
数据模型:
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
namespace XamDataGridUpdateTrigger
{
public class NamesModel : NamesDataCollection
{
public NamesModel()
{
}
}
public class NamesDataCollection : ObservableCollection<NamesItem>
{
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
base.OnCollectionChanged(e);
}
}
public class NamesItem : INotifyPropertyChanged
{
public NamesItem()
{
}
public NamesItem(string fname, string lname)
{
FirstName = fname;
LastName = lname;
}
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { if (_firstName != value) { _firstName = value; OnPropertyChanged("FirstName"); } }
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set { if (_lastName != value) { _lastName = value; OnPropertyChanged("LastName"); } }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
this.PropertyChanged?.Invoke(sender, e);
}
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
OnPropertyChanged(this, e);
}
protected virtual void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}