Hek*_*eki 9 wpf xaml wpf-controls controltemplate
该TextBlock元素处理LineHeight得很好,允许文本完全显示,而不会剪切.但是,我想将其更改TextBox为便于编辑文本,这是我的麻烦开始的地方.
在TextBlock显示文字是这样的:
在TextBox显示文字是这样的:
我试图摆弄ClipToBounds和Clip属性,但它只在元素内剪辑,不会扩展到边界之外.
LineHeight需要将该属性设置为低以调节线之间的差距,因此不能进行更改.
我也尝试过Padding,但它只是这样做
如果这是唯一的解决方案,那我就会走开,聆听按键并相应地更改文本,但这似乎是很多工作,我不认为这是一个很好的解决方案,所以这是我的浓缩题:
如何使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;
}
Run Code Online (Sandbox Code Playgroud)
它适用于a中的此控件 UserControl
<Grid>
...
<StackPanel>
<TextBox Background="Cornsilk" Name="Headline" AcceptsReturn="True" />
...
</StackPanel>
...
</Grid>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
这产生了非常理想的结果
感谢你们两位帮助我!
我可能在这里遗漏了一些东西,但是在调用文本框时,您可以设置高度和宽度的最小值和最大值,然后将它们设置为自动,例如
<TextBox Text="Test
test" Style="{StaticResource HeadlineFontTextBoxStyle}"
AcceptsReturn="True" Height="Auto" MinHeight="24" MaxHeight="120" />
Run Code Online (Sandbox Code Playgroud)
我应该指出,在堆栈面板中使用它会限制默认的高度,使用网格会给你最好的结果,因为它会动态扩展。
显然,您不希望控件无限期地扩展,因此如果控件变得太大,则 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
test" Style="{StaticResource HeadlineFontTextBoxStyle}" TextChanged="TextBox_TextChanged" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
作为参考,您可能需要从 MSDN 中查看: https ://msdn.microsoft.com/en-us/library/ms752068%28v=vs.85%29.aspx