WPF DataGrid的水平滚动条问题

问题描述 投票:1回答:3

我有以下数据网格:

<DataGrid x:Name="myDataGrid"
          RowHeaderWidth="{Binding RelativeSource={RelativeSource Self},
                           Path=RowHeight}">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Name" Width="*"
                        Binding="{Binding Name}"/>
    <DataGridTextColumn Header="Age" Width="1.2*"
                        Binding="{Binding Age}"/>
  </DataGrid.Columns>
</DataGrid>

<Button Grid.Row="1" Content="Add" Click="Button_Click"
        Width="100"/>

private void Button_Click(object sender, RoutedEventArgs e)
{
  var person = new Person()
  {
    Name = "Aaa",
    Age = 27
  };
  myDataGrid.Items.Add(person);
}

public class Person
{
  public string Name { get; set; }
  public int Age { get; set; }
}

问题是水平滚动条出现时一个新的行添加,这是不必要的。删除RowHeaderWidth属性将解决这个问题,但我需要它来显示验证错误。设置RowHeaderWidth为固定值也无济于事。可有人好心建议我一个孤子?

c# wpf xaml datagrid scrollbar
3个回答
0
投票

尝试这个:

        <DataGrid x:Name="myDataGrid"
      RowHeaderWidth="{Binding RelativeSource={RelativeSource Self},
                       Path=RowHeight}" ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Width="*" 
                    Binding="{Binding Name}"/>
                <DataGridTextColumn Header="Age" Width="*"
                    Binding="{Binding Age}"/>
            </DataGrid.Columns>
        </DataGrid>

0
投票

一个解决方法,我发现是设置一个按钮,它驻留在行和列(数据网格的左上)的交叉点的宽度。当第一行被添加到数据网格,此按钮出现在可视化树。我得知这个按钮here

public MainWindow()
{
  InitializeComponent();
  myDataGrid.ItemContainerGenerator.StatusChanged += onItemContainerGeneratorStatusChanged;
}

private void onItemContainerGeneratorStatusChanged(object sender, EventArgs e)
{
  if (((ItemContainerGenerator)sender).Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
  {
    Button btn = GetVisualChild<Button>(myDataGrid);
    if (btn != null)
    {
      btn.Width = myDataGrid.RowHeaderActualWidth;
    }
  }
}

public T GetVisualChild<T>(Visual 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;
}

如果我设置DataGrid.RowDetailsTemplate和DataGrid.SelectedItem到新添加的行它没有工作,虽然。所以,我试过如下:

private void onItemContainerGeneratorStatusChanged(object sender, EventArgs e)
{
  if (((ItemContainerGenerator)sender).Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
  {
    ScrollViewer sv = GetVisualChild<ScrollViewer>(myDataGrid);
    if (sv != null)
    {
      AutomationPeer automationPeer = FrameworkElementAutomationPeer.FromElement(sv);
      if (automationPeer == null)
        automationPeer = FrameworkElementAutomationPeer.CreatePeerForElement(sv);
      IScrollProvider provider = automationPeer.GetPattern(PatternInterface.Scroll) as IScrollProvider;
      try { provider.Scroll(ScrollAmount.SmallIncrement, ScrollAmount.NoAmount); }
      catch { }
      try { provider.Scroll(ScrollAmount.SmallDecrement, ScrollAmount.NoAmount); }
      catch { }
    }
  }
}

这确实解决了原来的问题,但引入了一个新问题:现在验证错误红框从文本框,它的错误,它们表明移动。


0
投票

供应我好另一个解决方法是:

private void fixScrollBarBug()
{
  ScrollBar scrollBar = GetChildByName<ScrollBar>(myDataGrid, "PART_HorizontalScrollBar");
  if (scrollBar != null)
  {
    if (VisualTreeHelper.GetChildrenCount(scrollBar) > 0)
    {
      Grid grid = (Grid)VisualTreeHelper.GetChild(scrollBar, 0);
      if (VisualTreeHelper.GetChildrenCount(grid) == 3)
      {
        try
        {
          RepeatButton leftButton = (RepeatButton)VisualTreeHelper.GetChild(grid, 0);
          RepeatButton rightButton = (RepeatButton)VisualTreeHelper.GetChild(grid, 2);

          AutomationPeer automationPeer = FrameworkElementAutomationPeer.FromElement(rightButton);
          if (automationPeer == null)
            automationPeer = FrameworkElementAutomationPeer.CreatePeerForElement(rightButton);
          IInvokeProvider provider = automationPeer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
          provider.Invoke();

          automationPeer = FrameworkElementAutomationPeer.FromElement(leftButton);
          if (automationPeer == null)
            automationPeer = FrameworkElementAutomationPeer.CreatePeerForElement(leftButton);
          provider = automationPeer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
          provider.Invoke();
        }
        catch { }
      }
    }
  }
}

打完电话后的第一行补充说,解决这一问题的上述方法:

myDataGrid.Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(
delegate
{
  fixScrollBarBug();
}));
© www.soinside.com 2019 - 2024. All rights reserved.