WPF:以编程方式引发一个带有对象的SelectionChangedEvent

时间:2018-10-22 10:04:45

标签: c# .net wpf selectionchanged

MouseDoubleClick打开一个新窗口,在其中我通过更改DataTable的内容来更新TextBoxes。更新DataTable之后,我需要提高SelectionChangedEvent才能将字符串更新为正确的值(在DataGrid中选择一行时,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();
        }
    }

1 个答案:

答案 0 :(得分:1)

必须将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();
            }
        }
    }