简体   繁体   中英

How to fix width of Column in DataGrid when Horizontal Scrollbar is Visible?

I using the complex header with datagrid like this . But I got problem of the scrollbar visibility it also spend a space so the width can not perfectly match with the grid. My Grid is just like this in <Column.Definition>

  <ColumnDefinition Width="{Binding ElementName=Column1, Path=ActualWidth}"/>
  <ColumnDefinition Width="{Binding ElementName=Column2, Path=ActualWidth}"/>
  <ColumnDefinition Width="{Binding ElementName=Column3, Path=ActualWidth}"/>
  <ColumnDefinition Width="{Binding ElementName=Column4, Path=ActualWidth}"/>
  <ColumnDefinition Width="{Binding ElementName=Column5, Path=ActualWidth}"/>
  <ColumnDefinition Width="{Binding ElementName=Column6, Path=ActualWidth}"/>
  <ColumnDefinition Width="{Binding ElementName=Column7, Path=ActualWidth}"/>
... till column 29

Anda I have data grid for Column.Definition like this

 <DataGrid.Columns>
<mui:DataGridTextColumn x:Name="Column1"  Width="50" Header="Segmen"  Binding="{Binding B4R1,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
<mui:DataGridTextColumn x:Name="Column2" Width="50" Header="Fisik" Binding="{Binding B4R2,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}" />
<mui:DataGridTextColumn x:Name="Column3" Width="50" Header="Sensus" Binding="{Binding B4R3,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}"/>
<mui:DataGridTextColumn x:Name="Column4" Width="50" Header="Tempat Tinggal" Binding="{Binding B4R4,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}" />
<mui:DataGridTextColumn x:Name="Column5" Width="50" Header="Campuran" Binding="{Binding B4R5,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}" />
.... till column29

The result is still like this :

How I can resolve the match width of column with scollbar visible like this? 在此输入图像描述

UPDATE

It still keep space in that although I have set the width..

在此输入图像描述

UPDATE 2

I don't know the scrollbar successfully resized but the space is still there 在此输入图像描述

Firstly, you can hide the Visibility of ScrollBar like this:

<DataGrid Name="dataGrid"
          ScrollViewer.VerticalScrollBarVisibility="Hidden" ... />

Secondly, you can set a fixed Width for ScrollBar :

<Window x:Class="MyClass.MainWindow"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"

<DataGrid Name="dataGrid">
    <DataGrid.Resources>
        <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">20</sys:Double>
        <sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">20</sys:Double>
    </DataGrid.Resources>
...
</DataGrid>

Thirdly, you can set the style for ScrollBar :

<DataGrid.Resources>
    <Style TargetType="{x:Type ScrollBar}">
        <Style.Triggers>                       
            <Trigger Property="Orientation" Value="Vertical">
                <Setter Property="Width" Value="10" />
                <Setter Property="MinWidth" Value="10" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.Resources>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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