1
votes

I have a WPF DataGrid and each row of the datagrid has an expander to show row details. Expander moves the scrollbar upon expanding the expander first time.

I don't want scroll bar to move when the expander is expanded. Any solution to resolve this?

So far I have tried following but it is not a good solution.

   private void Expander_Expanded(object sender, RoutedEventArgs e)
    {
        ScrollViewer scrollViewer = GetVisualChild<ScrollViewer>(dgUsers);
        scrollViewer.ScrollToHorizontalOffset(0);
    }

Since I do not have reputation right now I am not able to post the image.

Edit: Image link Attached

Image shows the scrollbar move

Edit:

I have attached the code sample so it would be easier for someone to recreate the issue on their end.

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<DockPanel Margin="10" LastChildFill="True">
    <Button Height="30" DockPanel.Dock="Top" Content="click me" Click="Button_Click"/>
    <DataGrid Name="dgUsers" AutoGenerateColumns="False"
              RowHeaderWidth="30"
              HeadersVisibility="All">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name1}" />
            <DataGridTextColumn Header="Birthday" Binding="{Binding Name1}" />
            <DataGridTextColumn Header="Name" Binding="{Binding Name1}" />
            <DataGridTextColumn Header="Birthday" Binding="{Binding Name1}" />
            <DataGridTextColumn Header="Name" Binding="{Binding Name1}" />
            <DataGridTextColumn Header="Name" Binding="{Binding Name1}" />
            <DataGridTextColumn Header="Birthday" Binding="{Binding Name1}" />
            <DataGridTextColumn Header="Name" Binding="{Binding Name1}" />
            <DataGridTextColumn Header="Birthday" Binding="{Binding Name1}" />

            <DataGridTextColumn Header="Birthday" Binding="{Binding Name1}" />
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <Expander Header="tewsting" ExpandDirection="Down"  Expanded="Expander_Expanded"
                          >
                    <DataGrid AutoGenerateColumns="True" RowHeaderWidth="20" HeadersVisibility="All" DataContext="{Binding}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name" Binding="{Binding Details}" />
                            <DataGridTextColumn Header="Birthday" Binding="{Binding Details}" />
                            <DataGridTextColumn Header="Name" Binding="{Binding Details}" />
                            <DataGridTextColumn Header="Birthday" Binding="{Binding Details}" />
                            <DataGridTextColumn Header="Name" Binding="{Binding Details}" />
                            <DataGridTextColumn Header="Name" Binding="{Binding Details}" />
                            <DataGridTextColumn Header="Birthday" Binding="{Binding Details}" />
                            <DataGridTextColumn Header="Name" Binding="{Binding Details}" />
                            <DataGridTextColumn Header="Birthday" Binding="{Binding Details}" />

                        </DataGrid.Columns>
                    </DataGrid>
                </Expander>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>
</DockPanel>

namespace WpfApplication2
{
public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        List<User> users = new List<User>();
        users.Add(new User() { Id1 = 1, Name1 = "asdfasdfadsfadsfadsfasdfasdf", Birthday = new DateTime(1971, 7, 23) });
        users.Add(new User() { Id1 = 2, Name1 = "Jane Doe", Birthday = new DateTime(1974, 1, 17) });
        users.Add(new User() { Id1 = 3, Name1 = "Sammy Doe", Birthday = new DateTime(1991, 9, 2) });

        dgUsers.ItemsSource = users;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ScrollViewer scrollViewer = GetVisualChild<ScrollViewer>(dgUsers);
        double Position = scrollViewer.ContentHorizontalOffset;
        Console.WriteLine(Position);
    }
    private static T GetVisualChild<T>(DependencyObject parent) where T : Visual
    {
        T child = default(T);

        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }

    private void Expander_Expanded(object sender, RoutedEventArgs e)
    {
        ScrollViewer scrollViewer = GetVisualChild<ScrollViewer>(dgUsers);
        scrollViewer.ScrollToHorizontalOffset(0);
    }
}

public class User
{
    public int Id1 { get; set; }
    public string Name1 { get; set; }
    public int Id2 { get { return 2000000; } }
    public string Name2 { get { return "aadafasdfasdsfasdfadsfadsfadsf"; } }
    public int Id3 { get { return 2000000; } }
    public string Name3 { get { return "aadafasdfasdsfasdfadsfadsfadsf;"; } }
    public int Id4 { get { return 2000000; } }
    public string Name4 { get { return "aadafasdfasdsfasdfadsfadsfadsf;"; } }
    public int Id5 { get { return 2000000; } }
    public string Name5 { get { return "aadafasdfasdsfasdfadsfadsfadsf;"; } }
    public int Id6 { get { return 2000000; } }
    public string Name6 { get { return "aadafasdfasdsfasdfadsfadsfadsf;"; } }
    public DateTime Birthday { get; set; }

    public string Details
    {
        get
        {
            return String.Format("{0} was born on {1} and this is a long description of the person.", this.Name1, this.Birthday.ToLongDateString());
        }
    }
}




}
1
post a link to imgur (or another hosting) in the question, or even a comment, and i'm sure someone will help you out.Noctis
please do post some XAML code for the same, it would be easier to understand the issue.pushpraj
Please post the xaml also the code for event handlers for the controls in the expanderAkanksha Gaur
@pushpraj I have posted the code.MChicago
Please help me with this one. I don't even see a link to offer bounty for this one.MChicago

1 Answers

0
votes

In case someone looks at this later, try setting the CanContentScroll property in the Scrollviewer equal to False.