我遵循了ChrisF here并编写了simple demo



而且我可以看到该按钮的幕后模板,如下面的屏幕截图所示(资源中的ButtonChrome)。

我想删除ButtonChrome 的白色渐变边框,并使按钮UI的其他所有内容保持不变。我怎样才能做到这一点?

ps。为了使问题更简单,我将ButtonChrome用作 View 上的普通控件。我浏览属性,但仍然不知道在哪里删除“白色”边框。

(正在使用的ButtonChrome)

(资源中的ButtonChrome)

最佳答案

我认为没有摆脱白色边界的简单方法。实际调用的例程是DrawInnerBorder,它是从ButtonChrome中的OnRender内部调用的。

protected override void OnRender(DrawingContext drawingContext)
{
    Rect bounds = new Rect(0.0, 0.0, base.ActualWidth, base.ActualHeight);
    this.DrawBackground(drawingContext, ref bounds);
    this.DrawDropShadows(drawingContext, ref bounds);
    this.DrawBorder(drawingContext, ref bounds);

    // This one draws the white Rectangle
    this.DrawInnerBorder(drawingContext, ref bounds);
}

private void DrawInnerBorder(DrawingContext dc, ref Rect bounds)
{
    if ((base.IsEnabled || this.RoundCorners) && ((bounds.Width >= 4.0) && (bounds.Height >= 4.0)))
    {
        Pen innerBorderPen = this.InnerBorderPen;
        if (innerBorderPen != null)
        {
            dc.DrawRoundedRectangle(null, innerBorderPen, new Rect(bounds.Left + 1.5, bounds.Top + 1.5, bounds.Width - 3.0, bounds.Height - 3.0), 1.75, 1.75);
        }
    }
}

如我们所见,没有属性可以禁用此功能。
您可以通过在ButtonChrome上设置IsEnabled="False"RoundCorners="False"或将Width或Height设置为3.99之类的方式来验证它,并且“白色边框”消失。

更新
要禁用ButtonChrome的内部边框,您可以创建自己的ButtonChrome并添加此属性。不幸的是ButtonChrome是密封的,所以我们不能从它继承。我试图复制整个类并添加Property DisableInnerBorder,它似乎正在工作。你可以这样使用
<local:MyButtonChrome ...
                      DisableInnerBorder="True">

除此之外,它的工作方式与常规ButtonChrome一样,您还可以添加其他可能需要的属性等。我没有想到过这种方法的缺点,但是对于一个小的测试来说,它工作得很好。

显然937行代码太多,无法在SO上发布:)我在这里上传MyButtonChrome.cs:http://www.mediafire.com/?wnra4qj4qt07wn6

10-07 13:18