我有一个WPF数据网格工具包。我想为数据网格中最后一列的单元格内容启用垂直滚动条,因为该内容有时太长。我还显示了一个工具提示。
我的问题是,当我选择该行时,没有选择最后一列,而其余列都是蓝色。参见下面的图片和代码:
<Window.Resources>
<Style x:Key="Body_Content_DataGrid_Centering" TargetType="{x:Type dg:DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type dg:DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<dg:DataGrid x:Name="dgDocs"
ItemsSource="{Binding Path=MyItemsSource, Mode=TwoWay}"
SelectedItem="{Binding Path=MySelectedItem, Mode=TwoWay}"
Margin="3 5 5 5"
AutoGenerateColumns="False"
CanUserAddRows="False" CanUserResizeRows="False"
SelectionMode="Single"
ColumnWidth="*"
AlternationCount="2" AlternatingRowBackground="Bisque"
Focusable="False" SelectionUnit="FullRow"
CellStyle="{StaticResource Body_Content_DataGrid_Centering}"
GridLinesVisibility="All"
HorizontalGridLinesBrush="{StaticResource ResDataGridGridlinesColor}"
VerticalGridLinesBrush="{StaticResource ResDataGridGridlinesColor}">
<dg:DataGrid.Resources>
<Style x:Key="DataGridTextColumnWithScrollBar"
TargetType="{x:Type dg:DataGridCell}"
BasedOn="{StaticResource {x:Type dg:DataGridCell}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type dg:DataGridCell}">
<TextBox Name="txtBox"
BorderBrush="Transparent" BorderThickness="0"
Text="{Binding Content.Text, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
VerticalScrollBarVisibility="Auto" TextWrapping="Wrap"
Background="Transparent" MaxHeight="24"
IsReadOnly="{TemplateBinding IsReadOnly}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</dg:DataGrid.Resources>
<dg:DataGrid.Columns>
<!-- Other columns -->
<dg:DataGridTextColumn x:Name="Result"
Header="Result"
MinWidth="60" Width="*"
IsReadOnly="True"
Binding="{Binding Path=Result, Converter={StaticResource ResultMessageConverter}}">
<dg:DataGridTextColumn.CellStyle>
<Style TargetType="dg:DataGridCell" BasedOn="{StaticResource DataGridTextColumnWithScrollBar}">
<Setter Property="ToolTip" Value="{Binding Path=Resultat, Converter={StaticResource ResultMessageConverter}}" />
</Style>
</dg:DataGridTextColumn.CellStyle>
</dg:DataGridTextColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
更新: 通过将文本框BorderBrush和Background更改为以下值({TemplateBinding Background}),部分解决了问题:
<dg:DataGrid.Resources>
<Style x:Key="DataGridTextColumnWithScrollBar"
TargetType="{x:Type dg:DataGridCell}"
BasedOn="{StaticResource {x:Type dg:DataGridCell}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type dg:DataGridCell}">
<TextBox Name="txtBox"
BorderBrush="{TemplateBinding Background}" BorderThickness="0"
Text="{Binding Content.Text, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
VerticalScrollBarVisibility="Auto" TextWrapping="Wrap"
Background="{TemplateBinding Background}" MaxHeight="24"
IsReadOnly="{TemplateBinding IsReadOnly}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</dg:DataGrid.Resources>
如果我为文本框设置的MaxHeight足够大(大于当前datagrid单元格的高度),则可以正常工作。但是,如果我为MaxHeight设置的值低于当前datagrid单元的高度,则无法正常工作,请参见下图。那么如何将文本框MaxHeight设置为当前datagrid单元格的高度?
在下面的图像中,您可以欣赏列单元格内容上方和下方的白色区域:
此外,在某些情况下,我注意到没有应用交替的行背景色。缺少一些东西。有什么想法吗?