Expander在wpf中移动滚动条

时间:2014-09-16 21:39:17

标签: wpf datagrid detailsview expander

我有一个WPF DataGrid,datagrid的每一行都有一个展开行详细信息的扩展器。扩展器在第一次扩展扩展器时移动滚动条。

当扩展器扩展时,我不希望滚动条移动。解决这个问题的任何解决方案?

到目前为止,我已尝试过以下但这不是一个好的解决方案。

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

由于我现在没有名声,我无法发布图片。

编辑:图片链接已附加

Image shows the scrollbar move http://i62.tinypic.com/24gvsb4.png

编辑:

我附上了代码示例,因此有人可以更轻松地重新创建问题。

<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 个答案:

答案 0 :(得分:0)

以防以后有人查看,请尝试将Scrollviewer中的CanContentScroll属性设置为False。