【发布时间】:2016-03-13 20:18:47
【问题描述】:
我的问题与this 问题有关。
这个问题已经过时了,而且答案更老了,感觉有点欠缺。用户从头开始创建了一个控件,但最大的一点是,它认为没有必要(至少对我而言)创建一个全新的控件来拥有一个可以支持的 TextBlock描边(轮廓)文本。
我试图通过提供一个 Brush 属性和一个 double 属性来扩展 TextBlock 控件。我曾希望我可以覆盖 OnRender 方法,但它是密封的,所以我不能。
这是我目前所拥有的(不多):
public class StrokedText: TextBlock {
public static readonly DependencyProperty
StrokeProperty = DependencyProperty.Register(
"Stroke",
typeof( Brush ),
typeof( StrokedText ),
new PropertyMetadata(
Brushes.Red,
( S, E ) => ( S as StrokedLabel ).InvalidateVisual( ) ) ),
StrokeWidthProperty = DependencyProperty.Register(
"StrokeWidth",
typeof( double ),
typeof( StrokedText ),
new PropertyMetadata(
2.0D,
( S, E ) => ( S as StrokedLabel ).InvalidateVisual( ) ) );
/// <summary>
/// Get or Set Stroke Brush.
/// </summary>
public Brush Stroke {
get { return this.GetValue( StrokeProperty ) as Brush; }
set { this.SetValue( StrokeProperty, value ); }
}
/// <summary>
/// Get or Set Stroke Width
/// </summary>
public double StrokeWidth {
get { return ( double )this.GetValue( StrokeWidthProperty ); }
set { this.SetValue( StrokeWidthProperty, value ); }
}
我已经花了一些时间查看 TextBlock 控件,以跟踪它在屏幕上实际将字符串呈现为文本的位置(我想如果我能找到我可以复制该方法),但我希望有人可能碰巧已经知道答案并节省了我一些时间,因为 TextBlock 控件只是......疯了。
那么 - 我是否可以扩展这个 TextBlock 以便我可以随心所欲地抚摸文本?
编辑 1:
为了清楚起见,对于我要做什么似乎存在误解 - 我不在乎概述文本块。我想概述 TEXT 本身。
【问题讨论】:
-
听起来你想使用效果。您的问题有点模糊,因为罢工和大纲是两种不同的操作。您想要文本周围的轮廓或突出的线条?
-
@SideriteZackwehdex 文本大纲。不删除。
-
@SideriteZackwehdex 我只看到两种效果(开箱即用)——模糊和阴影。我看不到“描边”效果选项。
-
我认为创建一个新控件几乎是不可避免的,您正在添加新的 DP,因此它们必须附加 DP,此时您的 XAML 将开始看起来有点混乱并且不会与 IDE 配合得很好。看看this page,它还是一个新控件,但是它继承了TextBlock并替换了它的渲染。
-
@MarkFeldman - 我会看看这个。我看到他们正在使用 TextBox - 我想使用 TextBLOCK - 我只是想显示数据,而不是促进数据交互。
标签: c# .net wpf xaml textblock