我有两把刷子。我不知道它们是什么类型的刷子。它们可以是 ImageBrushes、SolidBrushes 或 VisualBrushes。我有一个“画笔”类型的变量。
我需要结合两个画笔。我该怎么做?
我试过这个。但它没有用。这里是需要我组合的 Back 和 Front 的画笔。
Border Bd = new Border();
Border Bdr = new Border();
Bd.Width = 1.0;
Bd.Height = 1.0;
Bd.Background = Back;
Bdr.Background = Front;
Bd.Child = Bdr;
Brush VB = new VisualBrush(Bd);
我需要这个,因为我正在制作一个自定义动画类来为画笔设置动画。在进行了一些测试之后,我得出结论,错误在于画笔的组合,而不是课堂上的其他地方。
生成的画笔是完全透明的。
[编辑]
这是完整的 BrushAnimation 类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
namespace WPFSoPaTest
{
class BrushAnimation : AnimationTimeline
{
protected override Freezable CreateInstanceCore()
{
return new BrushAnimation();
}
public override Type TargetPropertyType
{
get { return typeof(Brush); }
}
static BrushAnimation()
{
FromProperty = DependencyProperty.Register("From", typeof(Brush),
typeof(BrushAnimation));
ToProperty = DependencyProperty.Register("To", typeof(Brush),
typeof(BrushAnimation));
}
public static readonly DependencyProperty FromProperty;
public Brush From
{
get
{
return (Brush)GetValue(BrushAnimation.FromProperty);
}
set
{
SetValue(BrushAnimation.FromProperty, value);
}
}
public static readonly DependencyProperty ToProperty;
public Brush To
{
get
{
return (Brush)GetValue(BrushAnimation.ToProperty);
}
set
{
SetValue(BrushAnimation.ToProperty, value);
}
}
public override object GetCurrentValue(object defaultOriginValue,
object defaultDestinationValue, AnimationClock animationClock)
{
Brush fromVal = ((Brush)GetValue(BrushAnimation.FromProperty)).CloneCurrentValue();
Brush toVal = ((Brush)GetValue(BrushAnimation.ToProperty)).CloneCurrentValue();
if ((double)animationClock.CurrentProgress == 0.0)
return fromVal; //Here it workes fine.
if ((double)animationClock.CurrentProgress == 1.0)
return toVal; //It workes also here fine.
toVal.Opacity = (double)animationClock.CurrentProgress;
Border Bd = new Border();
Border Bdr = new Border();
Bd.Width = 1.0;
Bd.Height = 1.0;
Bd.Background = fromVal;
Bdr.Background = toVal;
Bd.Visibility = Visibility.Visible;
Bdr.Visibility = Visibility.Visible;
Bd.Child = Bdr;
Brush VB = new VisualBrush(Bd);
return VB; //But here it return's a transparent brush.
//If I return the 'toVal' variable here it animates correctly the opacity.
}
}
}