如何修复因设置属性时递归而导致的此问题?

时间:2015-11-10 14:06:04

标签: c# wpf wpf-controls

我有这样的自定义控件:

public class UIButton : Button
{
    [EditorBrowsable(EditorBrowsableState.Never), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new Brush Background {get; set;}
    [EditorBrowsable(EditorBrowsableState.Never), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public Color MOBG { get; set; }
    [EditorBrowsable(EditorBrowsableState.Never), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public Color MDBG { get; set; }
    public Brush BackgroundBrush
    {
        get { return base.GetValue(BackgroundBrushProperty) as Brush; }
        set 
        {
            base.SetValue(BackgroundBrushProperty, value);
            MOBG = Functions.getLighterShade((GetValue(BackgroundBrushProperty) as SolidColorBrush).Color);
            MDBG = Functions.getDarkerShade((GetValue(BackgroundBrushProperty) as SolidColorBrush).Color);
        }
    }
    public static readonly DependencyProperty BackgroundBrushProperty =
      DependencyProperty.Register("BackgroundBrush", typeof(Brush), typeof(UIButton));

    public UIButton()
    {
        SetTemplate();
        SetEvents();
    }
    public void SetEvents()
    {
        this.PreviewMouseDown += UIButton_PreviewMouseDown;
    }

    void UIButton_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        Storyboard sb = new Storyboard();
        ColorAnimation c = new ColorAnimation();
        Storyboard.SetTargetProperty(c, new PropertyPath("BackgroundBrush.Color"));
        Storyboard.SetTarget(c, this);
        c.To = MDBG;
        c.Duration = new Duration(TimeSpan.FromSeconds(0.1));
        sb.Children.Add(c);
        sb.Begin();
    }
    public void SetTemplate()
    {
        ControlTemplate template = new ControlTemplate();
        FrameworkElementFactory root = new FrameworkElementFactory(typeof(Border)); root.Name = "border";
        root.SetBinding(Border.BackgroundProperty, new Binding("BackgroundBrush") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
        root.SetBinding(Border.WidthProperty, new Binding("Width") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
        root.SetBinding(Border.HeightProperty, new Binding("Height") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
        root.SetValue(Border.CornerRadiusProperty, new CornerRadius(5));

        FrameworkElementFactory content = new FrameworkElementFactory(typeof(TextBlock)); content.Name = "tx";
        content.SetValue(TextBlock.HorizontalAlignmentProperty, System.Windows.HorizontalAlignment.Center);
        content.SetValue(TextBlock.VerticalAlignmentProperty, System.Windows.VerticalAlignment.Center);
        content.SetBinding(TextBlock.TextProperty, new Binding("Content") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
        content.SetBinding(TextBlock.FontFamilyProperty, new Binding("FontFamily") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
        content.SetBinding(TextBlock.FontWeightProperty, new Binding("FontWeight") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
        content.SetBinding(TextBlock.FontSizeProperty, new Binding("FontSize") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
        content.SetValue(TextBlock.ForegroundProperty, Brushes.White);

        root.AppendChild(content);
        template.VisualTree = root;

        SetValue(TemplateProperty, template);
    }
}

我的职能类:

static class Functions
{
    public static Color getLighterShade(Color color)
    {
        float correctionFactor = 0.5f;
        float red = (255 - color.R) * correctionFactor + color.R;
        float green = (255 - color.G) * correctionFactor + color.G;
        float blue = (255 - color.B) * correctionFactor + color.B;
        System.Drawing.Color lighterColor = System.Drawing.Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
        return Color.FromArgb(lighterColor.A, lighterColor.R, lighterColor.G, lighterColor.B);
    }
    public static Color getDarkerShade(Color color)
    {
        System.Drawing.Color c1 = System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
        System.Drawing.Color c2 = System.Drawing.Color.FromArgb(c1.A,
            (int)(c1.R * 0.5), (int)(c1.G * 0.5), (int)(c1.B * 0.5));
        return Color.FromArgb(c2.A, c2.R, c2.G, c2.B);
    }
    public static LinearGradientBrush newGradientBrush(params Color[] colors)
    {
        GradientStopCollection gsc = new GradientStopCollection();
        float f = 1f / (float)(colors.Length - 1);
        float y = 0;
        for (int x = 0; x <= colors.Length - 1; x++)
        {
            gsc.Add(new GradientStop(colors[x], y));
            y += f;
        }
        return new LinearGradientBrush(gsc);
    }
}

最后是Extensions类:

public static class Extensions
{
    public static Color ToSysDrawCol(this System.Windows.Media.Color col)
    {
        return System.Drawing.Color.FromArgb(col.A, col.R, col.G, col.B);
    }
}

使用自定义控件,应该清楚我想说的是什么。

MOBGMDBG是鼠标悬停和鼠标悬停颜色。在BackgroundBrushProperty的设置器中,我根据当前背景的较浅和较暗的阴影设置这两种颜色。

现在如您所见,在MouseDown事件中,我为控件的背景属性设置了动画。但由于这一点,MDBG也再次发生变化。因此最终,颜色变得透明。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

不要设置MOBG&amp;设置MDBGBackgroundBrush,而是为两个属性实现get:

public Color MDBG 
{ 
    get
    {
            return  Functions.getDarkerShade((GetValue(BackgroundBrushProperty) as SolidColorBrush).Color);
    }; 
// no set
// set; 
}

如果Functions.getDarkerShade((GetValue(BackgroundBrushProperty) as SolidColorBrush).Color);太慢(我怀疑),请使用可用作缓存的成员变量Color? _MDBG(当它为null时计算一次,并为每个后续get返回值)。

在这种情况下,你必须在_MDBG = null;更改时实现正确的BackgroundBrush(即,不仅仅是设置时间,而是设置了与之前的值不同的值)