我知道这个话题已经被讨论过很多次了。我想我已经研究了其中的大部分。但我似乎无法使用 AddOwner() 完成这项工作。我是新手。
我希望制作一个非常简单的自定义控件来显示 FormattedText。即使从 XAML 调用依赖项属性,它们也不会被设置,并且它们的新设置在构造函数中不可用。
我究竟做错了什么?(也许问题应该是如何正确使用 AddOwner() ?)。
感谢您的任何帮助。
XAML:
<i:CustomTextBlock Grid.Row="0" x:Name="tbc"
Text="{Binding Text}"
FontSize="{Binding FontSize}"
FontStyle="Italic"
Foreground="{Binding Color}"
FontFamily="Segoe Script" />
C#:
public class CustomTextBlock : FrameworkElement
{
public CustomTextBlock()
{
FontWeight fontweight = FontWeights.Normal;
FontStretch fontstretch = FontStretches.Normal;
FlowDirection flowdirection = FlowDirection.LeftToRight;
Typeface typeface = new Typeface(
FontFamily,
FontStyle,
fontweight,
fontstretch);
FormattedText formattedText = new FormattedText(
Text,
System.Threading.Thread.CurrentThread.CurrentCulture,
flowdirection,
typeface,
FontSize,
Foreground);
}
public static readonly DependencyProperty TextProperty =
TextBlock.TextProperty.AddOwner(typeof(CustomTextBlock));
public String Text
{
get { return (String)this.GetValue(TextProperty); }
set { this.SetValue(TextProperty, value); }
}
public static readonly DependencyProperty FontSizeProperty =
TextBlock.FontSizeProperty.AddOwner(typeof(CustomTextBlock));
public Double FontSize
{
get { return (Double)this.GetValue(FontSizeProperty); }
set { this.SetValue(FontSizeProperty, value); }
}
public static readonly DependencyProperty FontStyleProperty =
TextBlock.FontStyleProperty.AddOwner(typeof(CustomTextBlock));
public FontStyle FontStyle
{
get { return (FontStyle)this.GetValue(FontStyleProperty); }
set { this.SetValue(FontStyleProperty, value); }
}
public static readonly DependencyProperty ForegroundProperty =
TextBlock.ForegroundProperty.AddOwner(typeof(CustomTextBlock));
public Brush Foreground
{
get { return (Brush)this.GetValue(ForegroundProperty); }
set { this.SetValue(ForegroundProperty, value); }
}
public static readonly DependencyProperty FontFamilyProperty =
TextBlock.FontFamilyProperty.AddOwner(typeof(CustomTextBlock));
public FontFamily FontFamily
{
get { return (FontFamily)this.GetValue(FontFamilyProperty); }
set { this.SetValue(FontFamilyProperty, value); }
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
}
}