- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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;
}
它应用于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 Text="Test
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
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/
; template person (deftemplate person (slot name (type STRING)) (slot gender (type SYMBOL)(allowed-s
我正在尝试使用 AudioRecord 从麦克风录制一些音频。录音有效,但音量太大,而且我的剪报很糟糕。我尝试使用 AutomaticGainControl,但它在我的设备上不可用。有没有其他方法可以
我正在开发一个小应用程序,用户可以在其中用手指在屏幕上移动背景。 background-position 由手指的位置定义。 我用的是angular,所以我有这种标签: 在我的 Cont
拥有 ag-grid v8.20 的 ag-grid(Angular 组件) 不幸的是,如果上下文菜单大于网格,它会被剪裁: (截图应该在这里,但我不能上传它,imgur bug?) 有没有办法让上下
我需要从 ListBox 中浮出一些内容如 DataTemplate 中指定的那样对于 ListBox.ItemTemplate .我正在使用 RenderTransform但内容被剪掉了 ListB
我有这个作业(我是学生),在 CLIPS 中,但是我无法取得任何进展,尽管在谷歌上搜索并花了一些时间。 (clear) (deftemplate book (multislot surname
我正在尝试在具有圆形路径的图像上使用“剪辑路径”。我知道有可能使用 svg 剪辑路径,但我认为它不可能让它真正响应 - 所以我决定在图像下方的 div 上使用 svg 图形 - 但我仍然有移动 Vie
我正在尝试在具有圆形路径的图像上使用“剪辑路径”。我知道有可能使用 svg 剪辑路径,但我认为它不可能让它真正响应 - 所以我决定在图像下方的 div 上使用 svg 图形 - 但我仍然有移动 Vie
背景 我们有一个 TIME 数据类型的列来指示耗时。 问题 当我们尝试插入大于 24 的小时数时,例如“25:00:00”,MySQL 会将此值裁剪为“01:00:00”,这不是我们想要的。 尝试的解
基本上,我正在创建一个包含两个图像的 View 。图像一显示在占据 View 左上角的直角三角形中,图像二显示在占据 View 右下角的直角三角形中。 想象一下沿对角线切割正方形,结果的每一半中存在不
我目前正在为单页网站制作剪切路径: http://grafomantestsite3.be/ 如您所见,这适用于 chrome,但不适用于 firefox、opera 等。 我做了一个代码笔来说明我的
我的 CSS 文件中有一行: clip: rect(0 0 0 0); 我看到 clip 现在已贬值,所以我尝试使用 clip-path。 clip-path 的等价物是什么? 是吗: clip-pa
我有两个 div,子 div 在父 div 中。 div child 比他的 parent 大。所以我决定在 div 父级中放置一个滚动条,因为我可以更好地看到 div 子级的内容。 问题是现在我需要
我正在为全屏背景图像在 WordPress 中编写 CSS。为了摆脱左侧的黑色矩形(菜单/侧边栏),我使用了下面的 CSS。 它在 chrome、IE 和移动设备上运行良好,但在 mac/safa
我想制作一个用户程序,它提取元素 a,其中元素 b(由参数给定)作为列表中的一对。 比如,如果我将 c 作为参数并列出 ((c a) (c b) (d f) (d g)),结果应该是 'a' 'b';
我是 Android 新手,我有两张图片,一张是空图片,另一张是代表进度条的完整图片。 如何使用 canvas.drawBitmap 只绘制整个图像的一部分? 我不想每次都调整位图图像的大小。 最佳答
我调整了我的字符串的大小以最大限度地适应高度(1 行),以便截断任何过大的宽度(如在字形中间),lineBreakMode:UILineBreakModeClip 应该这样做。相反,文本似乎通过仅绘制
我使用的导航 Controller 略微偏离屏幕边缘。当我尝试为导航 Controller 框架之外的 subview 设置动画时,它们会被剪裁。我试过设置: navigationController
我正在尝试使用 CSS3 转换为 CSS clip 设置动画,但没有成功。图像只是剪辑而没有过渡。 我错过了什么? #clipped { position:absolute; widt
我有一个 UITextView 显示一些大小可以变化的动态内容。textview 不允许滚动,它的大小与内容无关。考虑到它的自动布局约束,例如, TextView 在 iphone5 和 iPhone
我是一名优秀的程序员,十分优秀!