[英]C# / WPF - DataGrid - Binding the Width of a TextBox in RowDetails to that of the containing DataGrid
[英]C# WPF DataGrid Column Header fitting TextBox into width
我在我的 C# WPF 应用程序中有一个 DataGrid,并设置了 Columns Header 的样式,其下方带有一个标签/文本块和一个文本框作为即时过滤器。
我的目标是让 TextBox 的宽度适合 DataGridColumn 的宽度,但我没有找到适合我的方法。
目前我正在使用 Viewbox,但这会使 header 中元素的所有尺寸测量值崩溃。
如您所见,我的 TextBox 和 FontSize 太大或 TextBox 太小。
我目前的代码如下所示:
<DataGridTemplateColumn.Header>
<Viewbox StretchDirection="DownOnly">
<StackPanel>
<TextBlock Text="Mitarbeiter"/>
<TextBox x:Name="txtInstantSearchPersonio" HorizontalAlignment="Stretch" TextChanged="evh_InstantFilterChanged"/>
</StackPanel>
</Viewbox>
</DataGridTemplateColumn.Header>
有没有办法让 TextBox 完全适应列但不拉伸高度或字体大小?
您可以使用 DataGridColumnHeader 的样式来适应列内的文本框
<DataGridTemplateColumn Width="200">
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</DataGridTemplateColumn.HeaderStyle>
<DataGridTemplateColumn.Header>
<StackPanel>
<TextBlock Text="Mitarbeiter"/>
<TextBox x:Name="txtInstantSearchPersonio" HorizontalAlignment="Stretch" TextChanged="evh_InstantFilterChanged"/>
</StackPanel>
</DataGridTemplateColumn.Header>
绝望的尝试导致了一个非常简单的解决方案。
我将两个元素都放入 StackPanel:
<DataGridTemplateColumn.Header>
<StackPanel>
<TextBlock Margin="0" Padding="0" Text="Mitarbeiter"/>
<TextBox HorizontalAlignment="Stretch" TextChanged="evh_InstantFilterChanged"/>
</StackPanel>
</DataGridTemplateColumn.Header>
并将 DataGrid.ColumnHeaderStyle 简单地设置为:
<Style x:Key="style_DataGridColumnHeader" TargetType="DataGridColumnHeader">
<Style.Setters>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style.Setters>
</Style>
对我来说工作正常,TextBox 始终具有 header 列的全宽。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.