【问题标题】:ScrollIntoView won't trigger after ShowDialogShowDialog 后不会触发 ScrollIntoView
【发布时间】:2019-03-20 17:21:38
【问题描述】:

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

此时我完全迷失了。我怀疑它与在DataGrid 中加载DataTable 大约需要 500ms 的事实有关(我正在处理一些奇怪的查询)并且代码试图在可能之前移到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;
    }

【问题讨论】:

    标签: .net wpf datagridview datatable


    【解决方案1】:

    我通过添加 0 毫秒的延迟“解决了”这个问题。不要问我为什么有效。如果有人有解释,我将不胜感激。

    1. public DispatcherTimer Delay;
      
      public void DispatcherTimer()
      {
          Delay = new DispatcherTimer();
          Delay.Tick += DelayTick;
          Delay.Interval = new TimeSpan(0);
      }
      
    2. 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;
      }
      

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

    (一定要把ScrollIntoView改成ScrollToCenterOfView)

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

    不得不修改一小部分:

            // Compute the center point of the container relative to the scrollInfo
            System.Windows.Size size = container.RenderSize;
            System.Windows.Point center = container.TransformToAncestor((Visual)scrollInfo).Transform(new System.Windows.Point(size.Width / 2, size.Height / 2));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      相关资源
      最近更新 更多