ShowDialog

时间:2018-10-16 08:22:27

标签: .net wpf datagridview datatable

一旦我在辅助窗口中完成了数据编辑,代码便会触发。一切正常,但ScrollIntoView不会触发。它选择正确的Index,但随后拒绝滚动到它。

我现在完全迷失了。我怀疑它必须采取以下措施:将DataTable加载到DataGrid中需要大约 500毫秒(我正在处理一些奇怪的查询),并且代码试图在可能之前移至SelectedIndex

注意:“ dg_part.SelectedIndex = -1;”必须存在,否则我无法正确触发新的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 (Public_Strings.invokeDataGridParts == "yes")
            {
                InvokeDataGridPart();
                SqlPartsSetToRow(lastId);
                dg_part.ScrollIntoView(dg_part.Items[dg_part.SelectedIndex]);
                dg_part.SelectedIndex = -1;
            }
        }
    }

    public void InvokeDataGridPart()
    {
        SqlCommand cmd = new SqlCommand
        {
            CommandText = "SELECT * FROM cbu_deli WHERE [IDX] = '" + CurrentID + "' ORDER BY LEN ([DEL]), [DEL] ASC, [OPIS] DESC, [DELEZ] DESC",
            Connection = con
        };
        Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        dtPart.Clear();
        da.Fill(dtPart);
        dg_part.ItemsSource = dtPart.DefaultView;
        mycollection.GroupDescriptions.Clear();
        mycollection.GroupDescriptions.Add(new PropertyGroupDescription("DEL"));
        dg_part.ItemsSource = mycollection.View;
        Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
    }

    public int CurrentPartID
    {
        get
        {
            int tmp = 0;
            if (dg_part.SelectedIndex >= 0)
            {
                int.TryParse(dtPart.Rows[dg_part.SelectedIndex].ItemArray[0].ToString(), out tmp);
            }
            return tmp;
        }
    }


    public void SqlPartsSetToRow(int Id)
    {
        Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
        dg_part.SelectionChanged -= DG_Part_SelectionChanged;
        while (CurrentPartID != Id && dg_part.SelectedIndex < dtPart.Rows.Count - 1)
        {
            dg_part.SelectedIndex++;
        }
        dg_part.SelectionChanged += DG_Part_SelectionChanged;
        Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
    }

1 个答案:

答案 0 :(得分:0)

我通过添加0毫秒的延迟来“解决”它。不要问我为什么它起作用。如果有人有解释,我将不胜感激。

  1. focused
  2. public DispatcherTimer Delay;
    
    public void DispatcherTimer()
    {
        Delay = new DispatcherTimer();
        Delay.Tick += DelayTick;
        Delay.Interval = new TimeSpan(0);
    }
    

编辑:以下解决方案解决了我的问题并添加了居中:

(请确保将ScrollIntoView更改为ScrollToCenterOfView)

Make ListView.ScrollIntoView Scroll the Item into the Center of the ListView (C#)

不得不修改一个很小的部分:

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 (Public_Strings.invokeDataGridParts == "yes")
        {
            InvokeDataGridPart();
            SqlPartsSetToRow(lastId);
            Delay.Start();
        }
    }
}

public void DelayTick(object sender, EventArgs e)
{
    Delay.Stop();
    dg_part.ScrollIntoView(dg_part.Items[dg_part.SelectedIndex]);
    dg_part.SelectedIndex = -1;
}