Wpf 可以使用FormattedText.MinWidth设置文本框的宽度吗?
为了弄清楚这件事,我撞了头。目标是在给定位置创建一个包装文本框,其中宽度设置为文本字符串中最大单词的宽度。FormattedText.MinWidth计算字符串中最大单词的宽度。但是将MinWidth传递给TextBox会导致TextBox稍微过窄。TextBlock没有这个问题 很明显,文本框的深处发生了一些事情,导致了这种行为。我不能只是在文本框的宽度上添加一个固定的幻数,因为纠正问题所需的宽度的增加总是会根据包装到下一行的字符的像素宽度而有所不同。像素的数量将始终根据该字符是什么、字体和字体大小而有所不同 如果某人有更高的声誉,请添加FormattedText和MinWidth作为标记?限制不允许我,一个愚蠢的新手,这样做。我还想添加一个图像,这将使这个sooo更容易理解,但愚蠢的限制(我说了吗?)阻止我这样做Wpf 可以使用FormattedText.MinWidth设置文本框的宽度吗?,wpf,textbox,word-wrap,Wpf,Textbox,Word Wrap,为了弄清楚这件事,我撞了头。目标是在给定位置创建一个包装文本框,其中宽度设置为文本字符串中最大单词的宽度。FormattedText.MinWidth计算字符串中最大单词的宽度。但是将MinWidth传递给TextBox会导致TextBox稍微过窄。TextBlock没有这个问题 很明显,文本框的深处发生了一些事情,导致了这种行为。我不能只是在文本框的宽度上添加一个固定的幻数,因为纠正问题所需的宽度的增加总是会根据包装到下一行的字符的像素宽度而有所不同。像素的数量将始终根据该字符是什么、字体和字
namespace FormattedTextExample
{
public partial class FormattedText1 : Window
{
string noteText =
"We hold these truths to be self-evident, that all men are created equal, that they " +
"are endowed by their Creator with certain unalienable Rights, that among these are " +
"Life, Liberty and the pursuit of Happiness.--That to secure these rights, Governments " +
"are instituted..";
public FormattedText1()
{
InitializeComponent();
myText.Text = noteText;
}
protected override void OnRender(DrawingContext drawingContext)
{
FormattedText ft = new FormattedText(
textToFormat: noteText,
culture: CultureInfo.GetCultureInfo("en-us"),
flowDirection: myText.FlowDirection,
typeface: new Typeface(myText.FontFamily.ToString()),
emSize: myText.FontSize,
foreground: myText.Foreground);
ft.MaxTextWidth = ft.MinWidth;
DrawingContext dc = drawDest.Open();
dc.DrawText(ft, new Point(0, 0));
dc.Close();
myDrawingBrush.Drawing = drawDest;
myText.Width = ft.MinWidth;
}
}
}
<Window x:Class="FormattedTextExample.FormattedText1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="FormattedText" Height="500" Width="500">
<DockPanel>
<Grid DockPanel.Dock="Top" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0">
<Rectangle.Fill>
<DrawingBrush x:Name="myDrawingBrush" Stretch="None"
AlignmentY="Top" AlignmentX="Left" >
<DrawingBrush.Drawing>
<DrawingGroup x:Name="drawDest" />
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
<StackPanel Grid.Column="1">
<TextBox x:Name="myText" TextWrapping="Wrap" />
<!-- Everything works fine if using TextBlock -->
<!--<TextBlock x:Name="myText" TextWrapping="Wrap" />-->
</StackPanel>
</Grid>
</DockPanel>
</Window>
命名空间格式化文本示例
{
公共部分类FormattedText1:窗口
{
字符串注释文本=
“我们认为这些真理是不言而喻的,人人生而平等,人人平等”+
被造物主赋予某些不可转让的权利,其中包括+
生命、自由和对幸福的追求——为了保障这些权利,政府+
“设立……”;
公共格式化文本1()
{
初始化组件();
Text=noteText;
}
受保护的重写void OnRender(DrawingContext DrawingContext)
{
FormattedText ft=新的FormattedText(
text格式:noteText,
文化:CultureInfo.GetCultureInfo(“en-us”),
flowDirection:myText.flowDirection,
字体:新字体(myText.FontFamily.ToString()),
emSize:myText.FontSize,
前台:myText.前台);
ft.MaxTextWidth=ft.MinWidth;
DrawingContext dc=drawDest.Open();
dc.DrawText(英尺,新点(0,0));
dc.Close();
myDrawingBrush.Drawing=drawDest;
myText.Width=ft.MinWidth;
}
}
}
文本框需要更多空间的原因是其控件模板的定义方式
这是默认控件模板的外观(从):
研究默认控件模板或定义自己的模板有助于准确确定需要为文本框分配多少额外空间。尝试将
文本框设置为。Padding
属性设置为“0”
。查看默认模板(请参见我的答案),Padding属性不绑定任何内容,所以这没什么区别。如果我告诉你有一个答案让我很高兴,你会觉得我很奇怪。将Padding和BorderThickness设置为零,并在MinWidth中再添加五个像素,解决了这个问题!谢谢你令人惊叹的回答!酷:-)现在,如果你也接受我的答案,那么我也会很高兴;)事实上,谷歌“stackoverflow标记为已解决”,但仍然不清楚如何做到这一点。但这就是UI的本质,因为对一个人来说显而易见的东西可能会被另一个人忘记。
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border Name="Border"
BorderThickness="1"
CornerRadius="2"
Padding="2">
<Border.Background>
<SolidColorBrush Color="{DynamicResource ControlLightColor}" />
</Border.Background>
<Border.BorderBrush>
<SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
</Border.BorderBrush>
<!-- VisualStateManager code -->
<ScrollViewer x:Name="PART_ContentHost" Margin="0" />
</Border>
</ControlTemplate>