[英]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. 我正在使用WPF进行项目,并且有一个数据网格。 In the datagrid, I have 6 columns and I coded it to use and share all the width of datagrid. 在datagrid中,我有6列,并对其进行了编码以使用和共享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. 我想要发生的是为datagrid添加水平滚动,以便如果文本较长,则其标题的宽度将增加,因此,由于datagrid宽度除以其标题,因此将出现水平滚动。 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 XAML代码:
<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代替DataGridTextColumn,如下所示:
<DataGridTemplateColumn Header="Transaction" Width="160">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
<TextBlock Text="{Binding accountBalanceTransaction}"></TextBlock>
</ScrollViewer>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.