C# WPF:使用对象以编程方式引发SelectionChangedEvent

C# WPF:使用对象以编程方式引发SelectionChangedEvent,c#,.net,wpf,selectionchanged,C#,.net,Wpf,Selectionchanged,MouseDoubleClick打开一个新窗口,通过更改文本框的内容来更新DataTable。更新DataTable后,我需要启动SelectionChangedEvent,以便将字符串更新为正确的值(在数据网格中选择行时,SelectionChangedEvent会触发)。如果我在刷新DataGrid后没有以编程方式选择同一行,这就足够简单了,这意味着选择在技术上永远不会更改,并且除非我选择另一行,否则值不会更新 我通过将索引更改为-1然后将其更改回以前的值来解决这个问题,但我更愿意直接调用处

MouseDoubleClick
打开一个新窗口,通过更改
文本框的内容来更新
DataTable
。更新
DataTable
后,我需要启动
SelectionChangedEvent
,以便将字符串更新为正确的值(在数据网格中选择行时,SelectionChangedEvent会触发)。如果我在刷新
DataGrid
后没有以编程方式选择同一行,这就足够简单了,这意味着选择在技术上永远不会更改,并且除非我选择另一行,否则值不会更新

我通过将索引更改为-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)发送方进入
DataGrid gd=(DataGrid)dg_部分并重构逻辑:

    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
,然后调用
SelectionChanged
?我的意思是,内容只在
MouseDoubleClick
中更改,或者有另一种方法更新
数据表中的值?你能提供一个事件流程的例子吗?例如,我单击一个按钮并调用
MouseDoubleClick
。如果要处理值'-1',可以在
DG_Part_SelectionChanged
If(DG_Part.SelectedIndex=-1)返回@A.Wolf请看我的答案。我想我自己解决了。希望您能提供一两次建议的编辑,以供进一步参考。您所说的一两次建议的编辑是什么意思?如何改进代码?如何正确设置问题格式。我觉得很难理解。这正是我向你建议的;-)我经常使用这样的解决方案。