这个控件 继承的是一个 UserControl 类 ,在控件的 XAML 编辑 内放置了一个 Canvas的面板 面板里面 放置了 两个
TextBlock的控件,通过控制 两个 TextBlock 控件的偏移位置,使原先平面上的文字看起来有个阴影立体的效果
XAML编辑框中的代码
<Canvas>
<TextBlock x:Name="under" />
<TextBlock x:Name="above" />
</Canvas>
后台的代码:
#region 属性
#region SetOpacityForBottomProperty 获取或设置下层文字不透明度
private static readonly DependencyProperty SetOpacityForBottomProperty =
DependencyProperty.Register("SetOpacityForBottom",
typeof(double),
typeof(TextShadow),
new PropertyMetadata((double)1.0,
new PropertyChangedCallback(SetOpacityForBottomPropertyChanged)));
private static void SetOpacityForBottomPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextShadow local = d as TextShadow;
if (local != null)
{
local.TextOpacity();
}
}
/// <summary>
/// 获取或设置下层文字不透明度
/// </summary>
public double SetOpacityForBottom
{
get { return (double)GetValue(SetOpacityForBottomProperty); }
set { SetValue(SetOpacityForBottomProperty, value); }
}
#endregion
#region SetOpacityForTopProperty 获取或设置上层文字不透明度
private static readonly DependencyProperty SetOpacityForTopProperty =
DependencyProperty.Register("SetOpacityForTop",
typeof(double),
typeof(TextShadow),
new PropertyMetadata((double)1.0,
new PropertyChangedCallback(SetOpacityForTopPropertyChanged)));
private static void SetOpacityForTopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextShadow local = d as TextShadow;
if (local != null)
{
local.TextOpacity();
}
}
/// <summary>
/// 获取或设置上层文字的不透明度
/// </summary>
public double SetOpacityForTop
{
get { return (double)GetValue(SetOpacityForTopProperty); }
set { SetValue(SetOpacityForTopProperty, value); }
}
#endregion
#region SetColorForBottomProperty 获取或设置下层文字颜色
private static readonly DependencyProperty SetColorForBottomProperty =
DependencyProperty.Register("SetColorForBottom",
typeof(Brush),
typeof(TextShadow),
new PropertyMetadata(Brushes.Black,
new PropertyChangedCallback(SetColorForBottomPropertyChanged)));
private static void SetColorForBottomPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextShadow local = d as TextShadow;
if (local != null)
{
local.TextColor();
}
}
/// <summary>
/// 获取或设置下层文字颜色
/// </summary>
public Brush SetColorForBottom
{
get { return (Brush)GetValue(SetColorForBottomProperty); }
set { SetValue(SetColorForBottomProperty, value); }
}
#endregion
#region SetColorForTopProperty 获取或设置上层文字颜色
private static readonly DependencyProperty SetColorForTopProperty =
DependencyProperty.Register("SetColorForTop",
typeof(Brush),
typeof(TextShadow),
new PropertyMetadata(Brushes.White,
new PropertyChangedCallback(SetColorForTopPropertyChanged)));
private static void SetColorForTopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextShadow local = d as TextShadow;
if (local != null)
{
local.TextColor();
}
}
/// <summary>
/// 获取或设置上层文字颜色
/// </summary>
public Brush SetColorForTop
{
get { return (Brush)GetValue(SetColorForTopProperty); }
set { SetValue(SetColorForTopProperty, value); }
}
#endregion
#region SetTextProperty 获取或设置文字
private static readonly DependencyProperty SetTextProperty =
DependencyProperty.Register("SetText",
typeof(string),
typeof(TextShadow),
new PropertyMetadata((string)null,
new PropertyChangedCallback(SetTextPropertyChanged)));
private static void SetTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextShadow local = d as TextShadow;
var text = (string)e.NewValue;
local.above.Text = text;
local.under.Text = text;
}
/// <summary>
/// 获取或设置文字
/// </summary>
public string SetText
{
get { return (string)GetValue(SetTextProperty); }
set { SetValue(SetTextProperty, value); }
}
#endregion
#region SetLeftForTopProperty 获取或设置上层文字距左偏移量
private static readonly DependencyProperty SetLeftForTopProperty =
DependencyProperty.Register("SetLeftForTop",
typeof(double),
typeof(TextShadow),
new PropertyMetadata((double)0.0,
new PropertyChangedCallback(SetLeftForTopPropertyChanged)));
private static void SetLeftForTopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextShadow local = d as TextShadow;
if (local != null)
{
local.TextOffset();
}
}
/// <summary>
/// 获取或设置上层文字距左偏移量
/// </summary>
public double SetLeftForTop
{
get{return (double) GetValue(SetLeftForTopProperty);}
set{SetValue(SetLeftForTopProperty,value);}
}
#endregion
#region SetTopForTopProperty 获取或设置上层文字距上偏移量
private static readonly DependencyProperty SetTopForTopProperty =
DependencyProperty.Register("SetTopForTop",
typeof(double),
typeof(TextShadow),
new PropertyMetadata((double)0.0,
new PropertyChangedCallback(SetTopForTopPropertyChanged)));
private static void SetTopForTopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextShadow local = d as TextShadow;
if (local != null)
{
local.TextOffset();
}
}
/// <summary>
/// 获取或设置上层文字距上偏移量
/// </summary>
public double SetTopForTop
{
get { return (double)GetValue(SetTopForTopProperty); }
set { SetValue(SetTopForTopProperty, value); }
}
#endregion
#region SetLeftForBottomProperty 获取或设置下层文字距左偏移量
private static readonly DependencyProperty SetLeftForBottomProperty =
DependencyProperty.Register("SetLeftForBottom",
typeof(double),
typeof(TextShadow),
new PropertyMetadata((double)0.0,
new PropertyChangedCallback(SetLeftForBottomPropertyChanged)));
private static void SetLeftForBottomPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextShadow local = d as TextShadow;
if (local != null)
{
local.TextOffset();
}
}
/// <summary>
/// 获取或设置下层文字距左偏移量
/// </summary>
public double SetLeftForBottom
{
get { return (double)GetValue(SetLeftForBottomProperty); }
set { SetValue(SetLeftForBottomProperty, value); }
}
#endregion
#region SetTopForBottomProperty 获取或设置下层文字距上偏移量
private static readonly DependencyProperty SetTopForBottomProperty =
DependencyProperty.Register("SetTopForBottom",
typeof(double),
typeof(TextShadow),
new PropertyMetadata((double)0.0,
new PropertyChangedCallback(SetTopForBottomPropertyChanged)));
private static void SetTopForBottomPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextShadow local = d as TextShadow;
if (local != null)
{
local.TextOffset();
}
}
/// <summary>
/// 获取或设置下层文字距上偏移量
/// </summary>
public double SetTopForBottom
{
get { return (double)GetValue(SetTopForBottomProperty); }
set {SetValue(SetTopForBottomProperty,value);}
}
#endregion
#endregion
#region 方法
public void TextOffset()
{
SetTextOffset(above, under, SetLeftForTop, SetTopForTop, SetLeftForBottom, SetTopForBottom);
}
public void TextColor()
{
SetTextColor(above, under, SetColorForTop, SetColorForBottom);
}
public void TextOpacity()
{
SetTextOpacity(above, under, SetOpacityForTop,SetOpacityForBottom);
}
/// <summary>
/// 获取或设置文字偏移量
/// </summary>
private static void SetTextOffset(TextBlock above,TextBlock under, double aboveLeft,double aboveTop,double underLeft,double underTop)
{
if (above != null)
{
Canvas.SetLeft(above,aboveLeft);
Canvas.SetTop(above, aboveTop);
}
if (under != null)
{
Canvas.SetLeft(under, underLeft);
Canvas.SetTop(under, underTop);
}
}
/// <summary>
/// 获取或设置文字的颜色
/// </summary>
private static void SetTextColor(TextBlock above, TextBlock under, Brush aboveColor, Brush underColor)
{
if (above != null)
{
above.Foreground = aboveColor;
}
if (under != null)
{
under.Foreground = underColor;
}
}
/// <summary>
/// 获取或设置文字的不透明度
/// </summary>
private static void SetTextOpacity(TextBlock above, TextBlock under, double aboveOpacity, double underOpacity)
{
if (above != null)
{
above.Opacity = aboveOpacity;
}
if (under != null)
{
under.Opacity = underOpacity;
}
}
#endregion