1

The code is supposed to trigger once I'm done editing data in the secondary window. Everything works as it should, but the ScrollIntoView won't trigger. It selects the correct Index, but then refuses to scroll to it.

I'm completely lost at this point. I'm suspecting it has to do something with the fact that it takes about 500ms to load the DataTable in the DataGrid (I'm playing with some weird queries) and the code tries to move to the SelectedIndex before it is even possible?

Note: "dg_part.SelectedIndex = -1;" has to be there, or I can't trigger a new SelectionChanged event correctly.

Code:

    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;
    }
Siavash
  • 2,813
  • 4
  • 29
  • 42
Adephx
  • 187
  • 10

1 Answers1

0

I've "solved" it by adding a 0 ms delay. Don't ask me why it works. If someone has an explanation, I'd appreciate it.

  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;
    }
    

EDIT: The following solution solved my problem and added centering:

(Be sure to change ScrollIntoView into ScrollToCenterOfView)

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

Had to modify a tiny part:

        // 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));
Adephx
  • 187
  • 10