c# - 数据网格行标题未根据内容调整大小

标签 c# wpf xaml controltemplate datagridrowheader

我正在自定义 DataGrid,以便用户可以通过 TextBox 将信息直接输入到标题中。

我遇到的问题是当文本更改时,行标题没有调整大小以匹配内容的大小:

之前: enter image description here

之后: enter image description here enter image description here

如您所见,一旦文本框的大小已缩小以匹配新文本,标题就不会缩小以匹配文本框。

符合Minimal, Complete and Verifiable Example要求:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:l="clr-namespace:MCVE"
    xmlns:lib="clr-namespace:System;assembly=mscorlib"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Class="MCVE.MainWindow"
    mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <x:Array x:Key="Source" Type="{x:Type lib:String}">
            <lib:String>Foo</lib:String>
            <lib:String>Bar</lib:String>
            <lib:String>Baz</lib:String>
        </x:Array>
    </Window.Resources>
    <DataGrid
        AutoGenerateColumns="False"
        ItemsSource="{StaticResource Source}"
        RowHeight="50">
        <DataGrid.RowHeaderStyle>
            <Style TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <TextBox FontSize="36" HorizontalAlignment="Left" Text="This Is A Test" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.RowHeaderStyle>
    </DataGrid>
</Window>

在任何行标题中键入一些内容。然后清除它以重现。

那么我可以做些什么来强制行标题宽度并确保它保持尽可能小的宽度(不侵占实际的行标题内容)?

最佳答案

您可以处理 TextBox 元素的 SizeChanged 事件并跟踪它们的宽度。试试这个:

private readonly Dictionary<TextBox, double> _widths = new Dictionary<TextBox, double>();
private void TextBox_SizeChanged(object sender, SizeChangedEventArgs e)
{
    TextBox textBox = (TextBox)sender;
    _widths[textBox] = textBox.ActualWidth;

    double largestWidth = _widths.Values.Max();
    DataGridRowHeader header = FindParent<DataGridRowHeader>(textBox);
    dg.RowHeaderWidth = double.NaN;
    if (header != null)
        dg.RowHeaderWidth = dg.RowHeaderActualWidth > largestWidth ? largestWidth : double.NaN;
}

XAML:

<DataGrid.RowHeaderStyle>
    <Style TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Width" Value="Auto" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <TextBox FontSize="36" HorizontalAlignment="Left" Text="This Is A Test"
                                           SizeChanged="TextBox_SizeChanged" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.RowHeaderStyle>

关于c# - 数据网格行标题未根据内容调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50092381/

相关文章:

c# - 如何通过 VS.NET 自动化将文件打印到 C# 或更好的 XpsDocumentWriter?

wpf - 如何在 WPF 中流式传输视频?

c# - 属性更改时属性未将值传回文本框的问题

c# - 如何正确设置我的边框的动画以向右移动

c# - 有没有办法判断用户是否点击了屏幕上的任意位置?

c# - MouseEnter WPF 上的发光效果

c# - 要使用只读属性还是方法?

c# - 空列表或字典的内存使用情况?

c# - 制作一个类数组

c# - WPF 转 jpg/gif/png/svg