【发布时间】:2018-10-10 01:39:11
【问题描述】:
我正在自定义一个 DataGrid,以便用户可以通过 TextBox 将信息直接输入到标题中。
我遇到的问题是,当文本更改时,行标题没有调整大小以匹配内容的大小:
如您所见,一旦文本框的大小缩小以匹配新文本,标题不会缩小以匹配文本框。
符合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>
在任何行标题中输入一些内容。然后清除它以重现。
那么我可以做些什么来强制行标题宽度并确保它保持可能的最小宽度(不侵犯实际的行标题内容)?
【问题讨论】:
标签: c# wpf xaml controltemplate datagridrowheader