简体   繁体   English

WPF DataGrid 水平滚动条在列调整大小的右侧

[英]WPF DataGrid horizontal scrollbar snpas right on column resize

I have a problem with the default behavior of the resizing of columns:我对调整列大小的默认行为有疑问:

If a DataGrid is too wide for its container, the horizontal scrollbar appears.如果 DataGrid 对于其容器来说太宽,则会出现水平滚动条。 If I drag the bar to the right and resize most right column, the scrollbar sticks to the right.如果我向右拖动栏并调整最右侧列的大小,则滚动条会向右移动。

In my case I don't want that behavior.就我而言,我不想要这种行为。 The scrollbar should either just not stick to the right, or, perfect would be, a resize preview like MS Excel.滚动条要么不贴在右边,要么像 MS Excel 一样调整大小预览。

Can someone tell me how to achieve that?有人可以告诉我如何实现这一目标吗?

Edit1: This behavior is fine (not sticking to the right): Edit1:这种行为很好(不坚持正确): 常规滚动条行为

What I don't like is this:我不喜欢的是这个: 向右粘住/按扣

If I could realize that easily, I would prefer:如果我能很容易地意识到这一点,我宁愿: Excel 行为

/Edit1 /编辑1

I am using .Net 4.8 for a simple WPF application.我将 .Net 4.8 用于一个简单的 WPF 应用程序。

If an example is need, the following will display two grids and the left one can be used for that behavior:如果需要一个例子,下面将显示两个网格,左边的一个可用于该行为:

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <local:MasterViewModel/>
    </Window.DataContext>

    <DockPanel>
        <Button DockPanel.Dock="Bottom" Command="{Binding DisplaySelectionCountCommand}">Display Selection Count</Button>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            
            <DataGrid Grid.Column="0" ItemsSource="{Binding Items}" AutoGenerateColumns="False"
                 SelectionMode="Extended" local:MultiSelect.IsEnabled="True" HorizontalScrollBarVisibility="Auto">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="100"/>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="100"/>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="100"/>
                </DataGrid.Columns>
            </DataGrid>
            <DataGrid Grid.Column="1" ItemsSource="{Binding Items}"
                  SelectionMode="Extended" local:MultiSelect.IsEnabled="True"/>
        </Grid>
    </DockPanel>
</Window>

When I suggested to listen to the SizeChanged event I didn't mean the DataGrid to be the source.当我建议收听SizeChanged事件时,我并不是说DataGrid是源。

Since you are interested in the columns, you must listen to the cell event of course:既然你对列感兴趣,你当然必须听单元格事件:

MainWindow.xaml主窗口.xaml

<DataGrid>
  <DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
      <EventSetter Event="SizeChanged" Handler="DataGridCell_SizeChanged" />
    </Style>
  </DataGrid.CellStyle>
</DataGrid>

MainWindow.xaml.cs主窗口.xaml.cs

private void DataGridCell_SizeChanged(object sender, SizeChangedEventArgs e)
  => (sender as DataGridCell).BringIntoView();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM