wpf - 如何删除 ButtonChrome 边框(定义边框模板时)?

标签 wpf xaml button border

我关注了 ChrisF here并写一个 simple demo .

...open your project in Expression Blend, select the button and then right click and select "Edit Template > Edit a Copy..". This copies the existing template into one you can modify. It's easier if you create it in a resource dictionary.



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

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

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

(正在使用的 ButtonChrome)
alt text

(资源中的 ButtonChrome)
alt text

最佳答案

我不认为有一种简单的方法可以摆脱那个白色边框。实际调用的例程是 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);
        }
    }
}

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

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

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

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

关于wpf - 如何删除 ButtonChrome 边框(定义边框模板时)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4553692/

相关文章:

c# - FormattedText.BuildGeometry 删除字符

c# - 在 WPF 中打开关联文件,例如 'notepad++ tab features'

android - 如何在 Google android 应用程序后退按钮上做按钮点击动画?

python - Tkinter:当我在容纳按钮的框架旁边创建一个框架时,按钮会上下跳跃

wpf - 与 Entity Framework 绑定(bind)时,组合框的数据模板不会切换所选值

c# - 以编程方式向文本 block 添加一行

c# - UWP 动画汉堡图标

c# - 如何使用触摸将元素拖出可滚动的 ListView,而无需按住元素 [UWP]

c# - 更改命中区域

button - cocos2d通过按钮扩展触摸区域