我正在使用WPF进行项目,我有一个数据网格。在datagrid中,我有6列,我编写它来使用和共享datagrid的所有宽度。我的问题是,当一列具有足够长的标题宽度值时,长文本只是将文本剪切出来,只显示它的宽度可以有多少。我想要发生的是数据网格添加一个水平滚动,这样如果文本很长,其标题的宽度将增加,因此,由于数据网格宽度除以其标题,将出现水平滚动。请注意,其他标题的宽度不应调整或受此更改的影响,而应添加水平滚动。 下面是我的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>
提前致谢。
答案 0 :(得分:0)
您可以使用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>