WPF的DataGrid单元格的值更改事件 [英] WPF DataGrid cell value changed event
问题描述
我有一个设置,看起来像这样:
// mydg中的一个DataGrid的列DataGridTextColumn
的ObservableCollection< MyItem> MYOC;
// MYOC中填充了一些新的MyItem
myDG.ItemsSource = MYOC;
其中, MyItem
工具 INotifyPropertyChanged的
。什么时候的用户的输入一个值单元格中正确捕捉方法是什么?
我试着追赶的PropertyChanged
在 MyItem
S,但我周期性的背景也更新值(这个想法是,当用户手动编辑的值,一个标志被触发,它告诉定期计算,以避免覆盖手动输入的数据)。因此,的PropertyChanged
捕捉一切,包括定期更新,这是我不想要的。我想这可能使这项工作(通过设置当我做定期计算一个标志,那么检查的PropertyChanged事件处理程序不在标志的 - 但我想知道如果有一个简单的解决方案。)
我试着追赶 myDG.CurrentCellChanged
但是这触发每次用户改变小区选择,没有特别当他们编辑的单元格内容的时间
编辑:这里是XAML:
<数据网格X:NAME =mydg中的ItemsSource ={}绑定的AutoGenerateColumns =FALSE保证金=10,10,182,0VerticalAlignment =评出的HEIGHT =329ClipboardCopyMode =IncludeHeader>
< DataGrid.Columns>
< DataGridTextColumn标题=COL1绑定={结合为prop1}IsReadOnly =真/>
< DataGridTextColumn标题=col2的绑定={结合Prop2}IsReadOnly =FALSE/>
< /DataGrid.Columns>
< / DataGrid的>
下面是 MyItem
执行(使用< A HREF =https://github.com/Fody/PropertyChanged相对=nofollow> Fody / PropertyChanged的):
[ImplementPropertyChanged]
类MyItem:INotifyPropertyChanged的
{
公共事件PropertyChangedEventHandler的PropertyChanged;
公共字符串为prop1 {搞定;组; }
公共字符串Prop2 {搞定;组; }
公共MyItem()
{
为prop1 = Prop2 =;
}
}
的解决方案是赶 CellEditEnding
事件。
//初始化中
myDG.CellEditEnding + = myDG_CellEditEnding;
无效myDG_CellEditEnding(对象发件人,DataGridCellEditEndingEventArgs E)
{
如果(e.EditAction == DataGridEditAction.Commit)
{
柱VAR = E .Column为DataGridBoundColumn;
如果(列!= NULL)
{
VAR bindingPath =(column.Binding为绑定).Path.Path;
如果(bindingPath ==col2的)
{
INT的rowIndex = e.Row.GetIndex();
VAR EL = e.EditingElement为文本框;
// rowIndex位置有行索引
// bindingPath有柱的结合
// el.Text有新的,用户输入的值
}
$} b $ b}
}
I have a setup that looks like this:
// myDG is a DataGrid whose columns are DataGridTextColumn
ObservableCollection<MyItem> myOC;
// myOC is populated with some new MyItem
myDG.ItemsSource = myOC;
where MyItem
implements INotifyPropertyChanged
. What's the way to properly catch when the user inputs a value into a cell?
I've tried catching PropertyChanged
on the MyItem
s, but I also update the values periodically in the background (the idea is that when the user manually edits the value, a flag is triggered that tells the periodic calculation to avoid overwriting the manually entered data). So PropertyChanged
catches everything, including the periodic updates, which I don't want. I suppose it's possible to make this work (by setting a flag when I do the periodic calculation, then checking for absence of flag on the PropertyChanged event handler -- but I want to know if there's a simpler solution.)
I've tried catching myDG.CurrentCellChanged
but that's triggered every time the user changes the cell selection, not specifically when they edit cell contents.
Edit: Here is the XAML:
<DataGrid x:Name="myDG" ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="10,10,182,0" VerticalAlignment="Top" Height="329" ClipboardCopyMode="IncludeHeader">
<DataGrid.Columns>
<DataGridTextColumn Header="Col1" Binding="{Binding Prop1}" IsReadOnly="True"/>
<DataGridTextColumn Header="Col2" Binding="{Binding Prop2}" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
Here is the MyItem
implementation (uses Fody/PropertyChanged):
[ImplementPropertyChanged]
class MyItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public MyItem()
{
Prop1 = Prop2 = "";
}
}
The solution was to catch the CellEditEnding
event.
// In initialization
myDG.CellEditEnding += myDG_CellEditEnding;
void myDG_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
var column = e.Column as DataGridBoundColumn;
if (column != null)
{
var bindingPath = (column.Binding as Binding).Path.Path;
if (bindingPath == "Col2")
{
int rowIndex = e.Row.GetIndex();
var el = e.EditingElement as TextBox;
// rowIndex has the row index
// bindingPath has the column's binding
// el.Text has the new, user-entered value
}
}
}
}
这篇关于WPF的DataGrid单元格的值更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!