gpt4 book ai didi

wpf - 与 TextBlock 具有相同剪辑的 TextBox(TextBox 模板)

转载 作者:行者123 更新时间:2023-12-03 01:47:39 26 4
gpt4 key购买 nike

TextBlock 元素很好地处理了 LineHeight,允许文本完全显示,而无需剪切。但是,我想将其更改为 TextBox 以方便编辑文本,这就是我的麻烦开始的地方。

TextBlock 显示如下文本:

Tall letters display beyond the hight of the TextBlock

TextBox 显示如下文本:

Tall letters are being clipped!

我尝试摆弄 ClipToBoundsClip 属性,但它仅在元素内进行剪辑,不会扩展到边界之外。

LineHeight 属性需要设置得较低,以调节线条之间的间隙,因此无法更改。

我也尝试过Padding,但它只能做到这一点

Padding, but text is still clipped

如果这是唯一的解决方案,我会不遗余力地监听按键并相应地更改文本,但这似乎需要大量工作,而且我认为这不是一个好的解决方案,所以这是我的浓缩问题:

如果可能的话,如何使 TextBox 不以与 TextBlock 相同的方式剪辑文本?

<小时/> 更新这是样式代码(我目前拥有的)及其应用位置。

private static Style GetFontTextBlock()
{
var style = new Style();
style.Setters.Add(new Setter(TextBlock.LineStackingStrategyProperty, LineStackingStrategy.BlockLineHeight));
style.Setters.Add(new Setter(TextBlock.IsHyphenationEnabledProperty, true));
style.Setters.Add(new Setter(TextBlock.TextWrappingProperty, TextWrapping.Wrap));
style.Setters.Add(new Setter(Control.BorderBrushProperty, null));
return style;
}

public static Style GetHeadline()
{
// ApplyFont sets the Control.FontFamilyProperty to Geogrotesque Condensed Regular.
// It's a purchased font so I can't supply it, unfortunately
var style = ApplyFont(new Style { BasedOn = GetFontTextBlock() });
style.Setters.Add(new Setter(TextBlock.FontSizeProperty, 140));
style.Setters.Add(new Setter(TextBlock.LineHeightProperty, 112));
return style;
}

它应用于UserControl内的此控件

<Grid>
...
<StackPanel>
<TextBox Background="Cornsilk" Name="Headline" AcceptsReturn="True" />
...
</StackPanel>
...
</Grid>

<小时/> 更新根据 Cadogis 的回答,样式 setter 代码如下:

public static Style GetHeadline(Enums.Enums.SheetSizes size, object triggerTarget)
[...]
style.Setters.Add(new Setter(TextBox.TemplateProperty, XamlReader.Parse(
// Breaks and indentation for readability
@"<ControlTemplate TargetType='TextBox'
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<DockPanel>
<Decorator Name='PART_ContentHost' />
</DockPanel>
</ControlTemplate>")));
return style;
}

这会产生非常理想的结果

TextBox with TextBlock clipping!

谢谢你们对我的帮助!

最佳答案

我可能在这里遗漏了一些东西,但是在调用文本框时,您可以设置高度和宽度的最小值和最大值,然后将它们设置为自动,例如

<TextBox Text="Test&#xA;test" Style="{StaticResource HeadlineFontTextBoxStyle}" 
AcceptsReturn="True" Height="Auto" MinHeight="24" MaxHeight="120" />

我应该指出,在堆栈面板中使用它会限制默认高度,使用网格会给你最好的结果,因为它会动态扩展。

显然,您不希望控件无限期地扩展,因此如果控件变得太大,则 max 属性将控制它,但“自动”应该允许它正确调整大小。抱歉,如果我完全没有捕获要点。

<小时/>

模板创意

不确定这是否正是您想要的,但它是一个修改后的文本框模板,用于在边框之外渲染(它使用装饰器而不是滚动查看器) - 我想我应该尝试提供一个可能的选项工作。

<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style x:Key="FontTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="TextBlock.LineStackingStrategy" Value="BlockLineHeight"></Setter>
<Setter Property="TextBlock.IsHyphenationEnabled" Value="True"></Setter>
<Setter Property="TextBlock.TextWrapping" Value="Wrap"></Setter>
<Setter Property="Control.BorderBrush" Value="{x:Null}"></Setter>
</Style>
<Style x:Key="HeadlineFontTextBoxStyle" TargetType="{x:Type TextBox}" >
<Setter Property="TextBlock.LineHeight" Value="100"></Setter>
<Setter Property="TextBlock.LineStackingStrategy" Value="BlockLineHeight"/>
<Setter Property="FontSize" Value="140"></Setter>
<Setter Property="AcceptsReturn" Value="True" />
<Setter Property="Height" Value="Auto"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<DockPanel>
<Decorator Name="PART_ContentHost" />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<TextBox Text="Test&#xA;test" Style="{StaticResource HeadlineFontTextBoxStyle}" TextChanged="TextBox_TextChanged" />
</Grid>

作为引用,您可能需要查看 MSDN 中的内容: https://msdn.microsoft.com/en-us/library/ms752068%28v=vs.85%29.aspx

关于wpf - 与 TextBlock 具有相同剪辑的 TextBox(TextBox 模板),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31647296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com