简体   繁体   中英

Why editing the data in a wpf datagrid dosen't raise the “set” event of the binded property?

This is a very simplified version of my code. I have a DataGrid binded to a DataTable:

<DataGrid ItemsSource="{Binding Path=DtTest}" />

and in my DataContext class I have the property:

public DataTable DtTest { get; set; }

If I modify the DataTable DtTest by code, the relative "set" event will fire. But if the users modify the data at runtime through the datagrid, the "set" event will not fire even if the data within the dataTable are correctly updated !

I need to raise a custom event when the "set" event is fired. By the way, This DataGrid behavior sounds very strange to me: If I use a textbox, for example, the "set" event is always fired when the text is edited by the user. What is the problem here? How can I implement this event following the cleanest way?

Why should that be the case?

Why would a change "inside" the DataTable trigger the setter of the member containing said DataTable ?

The setter will only get called by this line

this.DtTest = new DataTable();

using this line

this.DtTest.TableName = "Hello World";

it won't trigger the setter.

Your example with TextBox is different because there is no "inside" value that gets changed. The member it self gets modified, there is no TextProperty.Value which will get set but instead only the TextProperty gets set.

You could try to listen to an fitting event of the DataGrid: DataGrid Events

For example, CellEndEditing or RowEndEditing

Or listen to an fitting event of the DataTable: DataTable Events

For example, RowChanged or ColumnChanged

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM