简体   繁体   中英

c# wpf datagrid column width auto size add horizontal scroll if needed

I'm working on a project using WPF and I have a datagrid. In the datagrid, I have 6 columns and I coded it to use and share all the width of datagrid. My problem is that when one column has a value that is long enough for its header width, the long text just cut the text out and just show what it can have base on its width. What I want to happen is for the datagrid to add a horizontal scroll so that if the text is long, the width of its header will increase and therefore since the datagrid width is divided by its headers, a horizontal scroll will appear. Note that other headers' width should not adjust or be affected by this change instead a horizontal scroll should be added. Below is my DataGrid XAML code:

<DataGrid x:Name="accountsBalance_grd"
                  HorizontalAlignment="Left"
                  Margin="266,118,0,0"
                  VerticalAlignment="Top"
                  Height="498"
                  Width="836"
                  AreRowDetailsFrozen="True"
                  AutoGenerateColumns="False"
                  CanUserReorderColumns="False"
                  CanUserResizeColumns="False"
                  CanUserResizeRows="False"
                  CanUserSortColumns="False"
                  CanUserAddRows="False"
                  BorderBrush="Blue"
                  Background="White"
                  Foreground="Blue"
                  BorderThickness="2"
                  HorizontalGridLinesBrush="Black"
                  Loaded="accountsBalance_grd_Loaded">
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="FontSize"
                            Value="20" />
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name"
                                    Width="150"
                                    Binding="{Binding accountBalanceName}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Total Balance"
                                    Width="150"
                                    Binding="{Binding accountBalanceTotal}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Amount"
                                    Width="120"
                                    Binding="{Binding accountBalanceAmount}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Transaction"
                                    Width="160"
                                    Binding="{Binding accountBalanceTransaction}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Date"
                                    Width="*"
                                    Binding="{Binding accountBalanceDate}"
                                    IsReadOnly="True" />
                <DataGridTextColumn Header="Incharge"
                                    Width="*"
                                    Binding="{Binding accountBalanceAdmin}"
                                    IsReadOnly="True" />
            </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="FontSize"
                            Value="15" />
                    <Setter Property="FontFamily"
                            Value="Arial" />
                    <Setter Property="FontWeight"
                            Value="Bold" />
                    <Setter Property="Foreground"
                            Value="Black" />
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>

Thanks in advance.

In place of DataGridTextColumn you can use DataGridTemplateColumn as follows:

<DataGridTemplateColumn Header="Transaction" Width="160">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
                <TextBlock Text="{Binding accountBalanceTransaction}"></TextBlock>
            </ScrollViewer>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

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