c# / WPF - DataGrid -将RowDetails中文本框的宽度绑定到包含DataGrid的宽度
本文关键字:DataGrid 包含 绑定 中文 WPF RowDetails 文本 | 更新日期: 2023-09-27 18:04:04
我的问题和这个类似;
- DataGrid RowDetails宽度问题
除非我希望行详细信息永远不要超过它所跨越的列的宽度。
|--0--|--1--|--2--|--3--|--4--|
|---------Row-Details---------|
我试过AreRowDetailsFrozen
,这没有效果。我也尝试绑定到父网格的实际宽度(单向),但这会导致宽度超过我的两个屏幕。
这是我当前的尝试(简化);
<Grid>
<DataGrid x:Name="Grid"
Grid.Row="1"
ItemsSource="{Binding Collection}"
IsReadOnly="True"
AutoGenerateColumns="False"
ColumnWidth="Auto"
CanUserResizeColumns="False"
CanUserResizeRows="False"
RowDetailsVisibilityMode="VisibleWhenSelected"
AreRowDetailsFrozen="True"
SelectionUnit="FullRow"
VerticalAlignment="Top"
HorizontalAlignment="Center">
<DataGrid.RowDetailsTemplate>
<!-- Begin row details section. -->
<DataTemplate>
<TextBox DataContext="{Binding ErrorMessage}"
IsReadOnly="True"
Margin="5"
BorderBrush="Transparent"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True"
TextWrapping="Wrap"
Text="{Binding .}">
</TextBox>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
结果如下;
|--0--|--1--|--2--|--3--|--4--|
|---------Row-Details are as wide as the longest row in their content ---------|
将TextBox的宽度绑定到任何父容器(Grid, DataGrid, ItemsPresenter):
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=ActualWidth, Mode=OneWay}"
结果:
|------Viewable Area-------|
|---- Columns ----|
|---------Row-Details --------------------------------------------------------------|
这是非常令人沮丧的,我只是想行细节不改变数据网格的宽度,是这么多的要求吗?:)
完成此操作的唯一方法是更改DataGridRow ControlTemplate。在那里,我们可以将行细节主机宽度(DataGridDetailsPresenter)绑定到单元格的宽度。例如:
<Style x:Key="{x:Type dg:DataGridRow}" TargetType="{x:Type dg:DataGridRow}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
<Setter Property="ValidationErrorTemplate">
<Setter.Value>
<ControlTemplate>
<TextBlock Margin="2,0,0,0" VerticalAlignment="Center" Foreground="Red" Text="!" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type dg:DataGridRow}">
<Border x:Name="DGR_Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
<dgp:SelectiveScrollingGrid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<dgp:DataGridCellsPresenter x:Name="cellPresenter" Grid.Column="1"
ItemsPanel="{TemplateBinding ItemsPanel}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<dgp:DataGridDetailsPresenter dgp:SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding RelativeSource={RelativeSource AncestorType={x:Type dg:DataGrid}}, Path=AreRowDetailsFrozen, Converter={x:Static dg:DataGrid.RowDetailsScrollingConverter}, ConverterParameter={x:Static dg:SelectiveScrollingOrientation.Vertical}}"
Grid.Column="1" Grid.Row="1"
Visibility="{TemplateBinding DetailsVisibility}" Width="{Binding ElementName=cellsPresenter, Path=ActualWidth}"/>
<dgp:DataGridRowHeader dgp:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Grid.RowSpan="2"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type dg:DataGrid}}, Path=HeadersVisibility, Converter={x:Static dg:DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static dg:DataGridHeadersVisibility.Row}}"/>
</dgp:SelectiveScrollingGrid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我在这里回答了一个类似的问题DataGrid RowDetails宽度问题
这里的答案感觉像是一个变通办法,所以我做了一些研究在Telerik论坛上找到解决方案,因为我们使用他们的RadGridView。结果这个解决方案也适用于DataGrid。
关键是设置ScrollViewer。HorizontalScrollBarVisibility属性设置为Disabled,参见下面的示例。
<DataGrid ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <DataGrid.RowDetailsTemplate> <DataTemplate> <Border> <TextBlock Foreground="White" Text="{Binding RowDetails}" TextWrapping="Wrap"/> </Border> </DataTemplate> </DataGrid.RowDetailsTemplate> </DataGrid>
我找到了另一种解决问题的方法:
private void GridOnLoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
var dataGridColumnHeadersPresenter = FindVisualChild<DataGridColumnHeadersPresenter>((DataGrid)sender);
e.DetailsElement.SetBinding(WidthProperty, new Binding("ActualWidth") { Source = dataGridColumnHeadersPresenter });
}
这可以防止您使用常数值(例如:'6') -这将不适合,如果有人设置DataGrid.RowHeaderWidth
-和转换器。
我已经将此添加到DataGrid.LoadingRowDetails
事件处理程序中,因为我已经以其他方式调整了RowDetails。