【发布时间】:2014-05-22 10:55:45
【问题描述】:
我创建了自定义 TextBlock 控件:
[ContentProperty("Text")]
[TemplatePart(Name = TextBlockName, Type = typeof(TextBlock))]
public class CustomTextBlock : Control
{
#region Constraints
private const string TextBlockName = "Container";
#endregion
#region Private Fields
private TextBlock _tbValue;
#endregion
#region TextProperty Depenancy Property
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text",
typeof(string),
typeof(CustomTextBlock),
new PropertyMetadata(null, TextChangedCallback));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
private static void TextChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var control = (CustomTextBlock)dependencyObject;
control.UpdateText();
}
#endregion
#region TextWrapping Dependency Property
public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register(
"TextWrapping",
typeof(TextWrapping),
typeof(CustomTextBlock), null);
public TextWrapping TextWrapping
{
get { return (TextWrapping)GetValue(TextWrappingProperty); }
set { SetValue(TextWrappingProperty, value); }
}
#endregion
public static readonly DependencyProperty TextStyleProperty =
DependencyProperty.Register("TextStyle", typeof(Style), typeof(CustomTextBlock), new PropertyMetadata(null));
public Style TextStyle
{
get { return (Style)GetValue(TextStyleProperty); }
set { SetValue(TextStyleProperty, value); }
}
#region TextAlignment Dependency Property
public static readonly DependencyProperty TextAlignmentProperty = DependencyProperty.Register(
"TextAlignment",
typeof(TextAlignment),
typeof(CustomTextBlock),
null);
public TextAlignment TextAlignment
{
get { return (TextAlignment)GetValue(TextAlignmentProperty); }
set { SetValue(TextAlignmentProperty, value); }
}
#endregion
#region Constructor
public CustomTextBlock()
{
DefaultStyleKey = typeof(CustomTextBlock);
}
#endregion
#region Methods
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_tbValue = GetTemplateChild(TextBlockName) as TextBlock;
UpdateText();
UpdateTextStyle();
}
private void UpdateTextStyle()
{
if (_tbValue != null && TextStyle != null)
_tbValue.Style = TextStyle;
}
private void UpdateText()
{
if (this._tbValue != null)
{
this._tbValue.Inlines.Clear();
if (!String.IsNullOrEmpty(this.Text))
{
// some stuff
}
}
}
#endregion
}
模板如下:
<Style TargetType="customControls:CustomTextBlock" >
<Setter Property="Foreground" Value="{StaticResource DefaultFontBrush}"/>
<Setter Property="FontSize" Value="30"/>
<Setter Property="FontFamily" Value="{StaticResource DefaultFont}"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="TextAlignment" Value="Left"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="customControls:CustomTextBlock">
<TextBlock Name="Container"
TextWrapping="{TemplateBinding TextWrapping}"
TextAlignment="{TemplateBinding TextAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
一切都很好,但是当我尝试使用 TextStyle 和其他属性时出现问题。 我的期望是这些特定属性会覆盖 TextStyle 中的属性,但事实并非如此。
<control:CustomTextBlock
TextStyle="{StaticResource StyleWithYellowForeground}"
Foreground="Red"
FontSize="20"
Text="Sample text"
/>
样式“StyleWithYellowForeground”有一些设置器,但它的前景设置为黄色。在我的声明中,我将前景设置为红色,因为我想以这种样式覆盖“前景”,但它不起作用(它仍然具有黄色前景)。
你知道如何解决这个问题吗?
我的意思是实现声明样式的某些优先级(从最重要的覆盖其他样式):
1)控件声明中设置的内联属性 2)控件声明中设置的TextStyle属性 3)默认控件样式中设置的默认属性
【问题讨论】:
标签: wpf xaml windows-phone-7 windows-phone-8 windows-phone