Wpf datagrid列大小未根据行

时间:2018-02-01 04:00:36

标签: c# wpf wpfdatagrid

Wpf datagrid列大小未根据行中存在的内容宽度进行调整

我们在wpf应用程序中有一个数据网格。

<DataGrid x:Name="dgvOrders" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Visibility="Visible" Cursor="Hand"
       CanUserAddRows="False"
       ColumnWidth="Auto"
       FontWeight="Normal" 
       Margin="5,5,5,0" 
       AlternationCount="2" 
       DragOver="dgvOrders_DragOver" 
       Drop="dgvOrders_Drop"
       DragEnter="dgvOrders_DragEnter"
       AllowDrop="True"
       Height="Auto"
       MaxHeight="380"
       PreviewMouseLeftButtonDown="dgvOrders_PreviewMouseLeftButtonDown"
       SelectionMode="Extended" GridLinesVisibility="Horizontal" 
       HorizontalGridLinesBrush="#FFD8D8D8" VerticalGridLinesBrush="#FFD8D8D8"
       PreviewKeyDown="dgvOrders_PreviewKeyDown" 
       AutoGenerateColumns="False">
       <DataGrid.RowStyle>
           <Style TargetType="DataGridRow">
            <Style.Triggers>
                  <DataTrigger Binding="{Binding IsDisposed}" Value="False">
                       <Setter Property="Background" Value="LightGreen" />
                  </DataTrigger>
            </Style.Triggers>
           </Style>
        </DataGrid.RowStyle>
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="dgcRouteIndex" Binding="{Binding RouteOrderIndex, Mode=OneWay}" Header="Tour Reihenfolge" Width="Auto" Visibility="Hidden" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding FullName, Mode=OneWay}" Header="Name" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding Address.Zip, Mode=OneWay}" Header="PLZ" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding Address.City, Mode=OneWay}" Header="ORT" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding Address.Street, Mode=OneWay}" Header="Straße" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding BirthDate, StringFormat=\{0:dd.MM.yyyy\}, Mode=OneWay}" Header="Geburtsdatum" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding IsChild, Converter={StaticResource BooleanToJaNeinConverter},Mode=OneWay}" Header="Kind" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding IsSecondVisit, Converter={StaticResource BooleanToJaNeinConverter},Mode=OneWay}" Header="Verzögerung" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding DeadlineDate, StringFormat=\{0:dd.MM.yyyy\}, Mode=OneWay}" Header="Fristdatum" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding IsNoCurePossible, Converter={StaticResource BooleanToJaNeinConverter},Mode=OneWay}" Header="Anlass" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding IsSecondVisit, Converter={StaticResource BooleanToJaNeinConverter},Mode=OneWay}" Header="Gutachtenart" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding OpinionDate, StringFormat=\{0:dd.MM.yyyy\}, Mode=OneWay}" Header="Begutachtungsdatum" Width="Auto" CanUserResize="False"/>
            <DataGridTextColumn Binding="{Binding Info, Mode=OneWay}" Header="Info" Width="Auto"/>
            <DataGridTextColumn x:Name="dgcRouteDistance" Binding="{Binding RoutingDistance,Mode=OneWay}" Header="Kilometers" Width="Auto" Visibility="Hidden" CanUserResize="False"/>
           </DataGrid.Columns>
  </DataGrid>

我们具有按下控制键滚动鼠标滚轮的功能,应用程序中存在的文本字体大小增加(向上滚动:增加,向下滚动:减少)。

当我们向上滚动时,数据网格行中存在的文本字体大小会增加,并且相应列的宽度也会增加。

但是当我们向下滚动时,数据网格行中文本的字体大小会减小,但列宽不会减少。

任何人都有解决方案吗?

1 个答案:

答案 0 :(得分:0)

我正在使用此循环

foreach (ColumnHeader c in table.Columns)
{
    c.Width = -2;
}

宽度-2将宽度调整为标题和单元格的内容。