使用WPF& MVVM,我有以下数据网格,有两列:
<DataGrid Name="RateTableDataGrid"
ItemsSource="{Binding RateTableDataView, UpdateSourceTrigger=PropertyChanged}"
IsSynchronizedWithCurrentItem="True"
AutoGenerateColumns="False"
CanUserAddRows="False"
MaxHeight="300">
<DataGridTextColumn Header="Rate Table"
Width="75"
Binding="{Binding RateTable}"
ElementStyle="{StaticResource CenterTextCellStyle}">
</DataGridTextColumn>
<DataGridComboBoxColumn Header="Match Type"
Width="100"
ItemsSource="{Binding Source={StaticResource MatchTypeCollection}}"
SelectedValueBinding="{Binding MatchType, UpdateSourceTrigger=PropertyChanged}">
</DataGridComboBoxColumn>
</DataGrid>
DataGrid绑定到DataView,并设置为侦听ListChanged事件的更改:
/// <summary>
/// Dataview of Rate Tables data used by datagrid
/// </summary>
public DataView RateTableDataView
{
get
{
DataSet ds = new DataSet("RateTables");
try
{
using (SQLClass _sql = new SQLClass(_config.DatabaseInfo))
{
using (SqlConnection _conn = _sql.Connection)
{
if (SelectedPayrollDatabase != null)
{
// use the chosen Payroll Database from ComboBox
SqlConnectionStringBuilder _newConn =
new SqlConnectionStringBuilder(_conn.ConnectionString) { InitialCatalog = SelectedPayrollDatabase };
_conn.ConnectionString = _newConn.ConnectionString;
// Create new sql command
SqlCommand _cmd = _conn.CreateCommand();
_cmd.CommandType = CommandType.StoredProcedure;
_cmd.CommandText = "aem.S_RateTable_SP";
// create new sql data adapter
SqlDataAdapter _adapter = new SqlDataAdapter(_cmd);
// Fill the temporary dataset with the data from the data adapter
_adapter.Fill(ds);
// Set dataview data to that of dataset table
_RateTableDataView = ds.Tables[0].DefaultView;
// Add listener for changes to the RateTableDataView
_RateTableDataView.ListChanged += new ListChangedEventHandler(RateTableChanged);
// Rate table is always clean at this point
RateTableIsDirty = false;
}
}
}
}
}
这样可以调用ListChanged事件,但只有当我将焦点更改为表中的另一行数据时才会调用。
我希望能够执行一个方法(存储在VM中),只要这些列中的任何一个的值发生更改而不需要用户选择其他行,就会更新另一个列。
我尝试在每个列中添加一个EventTrigger(在另一篇文章中找到):
<DataGridTextColumn Header="Rate Table"
Width="75"
Binding="{Binding RateTable}"
ElementStyle="{StaticResource CenterTextCellStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellEditEnded">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="RateTableCellChanged" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGridTextColumn>
但从不调用RateTableCellChanged(即使将焦点更改为其他行)。
当文本或下拉选项发生变化而没有离开行时,有没有办法在VM中调用方法?