WPF:以编程方式引发带有对象的 SelectionChangedEvent
WPF: Raise programmatically a SelectionChangedEvent with an object
MouseDoubleClick
打开一个新的 window,我在其中通过更改 TextBoxes
的内容来更新 DataTable
。更新 DataTable
后,我需要引发 SelectionChangedEvent
以便将字符串更新为正确的值(当您 select DataGrid 中的一行时触发 SelectionChangedEvent)。如果我在刷新 DataGrid
后没有以编程方式 selecting 同一行,那将很简单,这意味着 selection 在技术上永远不会改变并且值不会更新除非我 select 另一行。
我通过将索引更改为 -1 然后将其更改回之前的值解决了这个问题,但我宁愿直接调用处理程序 DG_Part_SelectionChanged();
。将逻辑重构到一个新函数中是行不通的。
public void DG_Part_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (CurrentPartID != 0)
{
int lastId = CurrentPartID;
EditWindow ew = new EditWindow(CurrentPartID)
{
Owner = this
};
ew.ShowDialog();
if (Global.invokeDataGridParts == "yes")
{
// Refreshes the datagrid with an updated datatable
InvokeDataGridPart();
// Finds and selects the new index position of the modified row
SqlPartsSetToRow(lastId);
// Scrolls into view
dg_part.ScrollToCenterOfView(dg_part.Items[dg_part.SelectedIndex]);
// Highlights the row
Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(() =>
{
DataGridRow row = (DataGridRow)dg_part.ItemContainerGenerator.ContainerFromIndex(dg_part.SelectedIndex);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
));
// Restores index so that you may re-select the previous selection correctly
int saveIndex = dg_part.SelectedIndex;
dg_part.SelectedIndex = -1;
dg_part.SelectedIndex = saveIndex;
}
}
}
public void DG_Part_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid gd = (DataGrid)sender;
if (gd.SelectedItem is DataRowView row_selected)
{
Global.del = row_selected["DEL"].ToString();
Global.delez = row_selected["DELEZ"].ToString();
Global.cr_tu = row_selected["CRTU"].ToString();
Global.st_clanov = row_selected["ST"].ToString();
Global.lastnik = row_selected["LASTNIK"].ToString();
Global.naslov = row_selected["NASLOV"].ToString();
Global.ps = row_selected["PS"].ToString();
Global.obmocje2 = row_selected["OBMOCJE"].ToString();
Global.drzava = row_selected["DRZAVA"].ToString();
Global.emso = row_selected["EMSO"].ToString();
Global.maticna_st = row_selected["MATICNA"].ToString();
Global.reference = row_selected["REFERENCE"].ToString();
Global.opis = row_selected["OPIS"].ToString();
Global.opomba = row_selected["OPOMBA"].ToString();
}
}
必须将 DataGrid gd = (DataGrid)sender;
更改为 DataGrid gd = (DataGrid)dg_part;
并重构逻辑:
public void DG_Part_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DG_Part_Selection();
}
public void DG_Part_Selection()
{
DataGrid gd = (DataGrid)dg_part;
if (gd.SelectedItem is DataRowView row_selected)
{
Global.del = row_selected["DEL"].ToString();
Global.delez = row_selected["DELEZ"].ToString();
Global.cr_tu = row_selected["CRTU"].ToString();
Global.st_clanov = row_selected["ST"].ToString();
Global.lastnik = row_selected["LASTNIK"].ToString();
Global.naslov = row_selected["NASLOV"].ToString();
Global.ps = row_selected["PS"].ToString();
Global.obmocje2 = row_selected["OBMOCJE"].ToString();
Global.drzava = row_selected["DRZAVA"].ToString();
Global.emso = row_selected["EMSO"].ToString();
Global.maticna_st = row_selected["MATICNA"].ToString();
Global.reference = row_selected["REFERENCE"].ToString();
Global.opis = row_selected["OPIS"].ToString();
Global.opomba = row_selected["OPOMBA"].ToString();
}
}
然后只需调用处理程序:
public void DG_Part_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (CurrentPartID != 0)
{
int lastId = CurrentPartID;
EditWindow ew = new EditWindow(CurrentPartID)
{
Owner = this
};
ew.ShowDialog();
if (Global.invokeDataGridParts == "yes")
{
// Refreshes the datagrid with an updated datatable
InvokeDataGridPart();
// Finds and selects the new index position of the modified row
SqlPartsSetToRow(lastId);
// Scrolls into view
dg_part.ScrollToCenterOfView(dg_part.Items[dg_part.SelectedIndex]);
// Highlights the row
Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(() =>
{
DataGridRow row = (DataGridRow)dg_part.ItemContainerGenerator.ContainerFromIndex(dg_part.SelectedIndex);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
));
// Restores index so that you may re-select the previous selection correctly
DG_Part_Selection();
}
}
}
MouseDoubleClick
打开一个新的 window,我在其中通过更改 TextBoxes
的内容来更新 DataTable
。更新 DataTable
后,我需要引发 SelectionChangedEvent
以便将字符串更新为正确的值(当您 select DataGrid 中的一行时触发 SelectionChangedEvent)。如果我在刷新 DataGrid
后没有以编程方式 selecting 同一行,那将很简单,这意味着 selection 在技术上永远不会改变并且值不会更新除非我 select 另一行。
我通过将索引更改为 -1 然后将其更改回之前的值解决了这个问题,但我宁愿直接调用处理程序 DG_Part_SelectionChanged();
。将逻辑重构到一个新函数中是行不通的。
public void DG_Part_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (CurrentPartID != 0)
{
int lastId = CurrentPartID;
EditWindow ew = new EditWindow(CurrentPartID)
{
Owner = this
};
ew.ShowDialog();
if (Global.invokeDataGridParts == "yes")
{
// Refreshes the datagrid with an updated datatable
InvokeDataGridPart();
// Finds and selects the new index position of the modified row
SqlPartsSetToRow(lastId);
// Scrolls into view
dg_part.ScrollToCenterOfView(dg_part.Items[dg_part.SelectedIndex]);
// Highlights the row
Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(() =>
{
DataGridRow row = (DataGridRow)dg_part.ItemContainerGenerator.ContainerFromIndex(dg_part.SelectedIndex);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
));
// Restores index so that you may re-select the previous selection correctly
int saveIndex = dg_part.SelectedIndex;
dg_part.SelectedIndex = -1;
dg_part.SelectedIndex = saveIndex;
}
}
}
public void DG_Part_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid gd = (DataGrid)sender;
if (gd.SelectedItem is DataRowView row_selected)
{
Global.del = row_selected["DEL"].ToString();
Global.delez = row_selected["DELEZ"].ToString();
Global.cr_tu = row_selected["CRTU"].ToString();
Global.st_clanov = row_selected["ST"].ToString();
Global.lastnik = row_selected["LASTNIK"].ToString();
Global.naslov = row_selected["NASLOV"].ToString();
Global.ps = row_selected["PS"].ToString();
Global.obmocje2 = row_selected["OBMOCJE"].ToString();
Global.drzava = row_selected["DRZAVA"].ToString();
Global.emso = row_selected["EMSO"].ToString();
Global.maticna_st = row_selected["MATICNA"].ToString();
Global.reference = row_selected["REFERENCE"].ToString();
Global.opis = row_selected["OPIS"].ToString();
Global.opomba = row_selected["OPOMBA"].ToString();
}
}
必须将 DataGrid gd = (DataGrid)sender;
更改为 DataGrid gd = (DataGrid)dg_part;
并重构逻辑:
public void DG_Part_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DG_Part_Selection();
}
public void DG_Part_Selection()
{
DataGrid gd = (DataGrid)dg_part;
if (gd.SelectedItem is DataRowView row_selected)
{
Global.del = row_selected["DEL"].ToString();
Global.delez = row_selected["DELEZ"].ToString();
Global.cr_tu = row_selected["CRTU"].ToString();
Global.st_clanov = row_selected["ST"].ToString();
Global.lastnik = row_selected["LASTNIK"].ToString();
Global.naslov = row_selected["NASLOV"].ToString();
Global.ps = row_selected["PS"].ToString();
Global.obmocje2 = row_selected["OBMOCJE"].ToString();
Global.drzava = row_selected["DRZAVA"].ToString();
Global.emso = row_selected["EMSO"].ToString();
Global.maticna_st = row_selected["MATICNA"].ToString();
Global.reference = row_selected["REFERENCE"].ToString();
Global.opis = row_selected["OPIS"].ToString();
Global.opomba = row_selected["OPOMBA"].ToString();
}
}
然后只需调用处理程序:
public void DG_Part_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (CurrentPartID != 0)
{
int lastId = CurrentPartID;
EditWindow ew = new EditWindow(CurrentPartID)
{
Owner = this
};
ew.ShowDialog();
if (Global.invokeDataGridParts == "yes")
{
// Refreshes the datagrid with an updated datatable
InvokeDataGridPart();
// Finds and selects the new index position of the modified row
SqlPartsSetToRow(lastId);
// Scrolls into view
dg_part.ScrollToCenterOfView(dg_part.Items[dg_part.SelectedIndex]);
// Highlights the row
Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(() =>
{
DataGridRow row = (DataGridRow)dg_part.ItemContainerGenerator.ContainerFromIndex(dg_part.SelectedIndex);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
));
// Restores index so that you may re-select the previous selection correctly
DG_Part_Selection();
}
}
}