【问题标题】:How to apply Border to Button control programmatically using C#如何使用 C# 以编程方式将边框应用于按钮控件
【发布时间】:2020-06-11 13:00:57
【问题描述】:

我正在使用 C# 创建一个 Button 控件,如下面的代码中所述。我为按钮样式创建了圆形边框。我看不到任何属性来分配按钮中的边框。

   var button = new System.Windows.Controls.Button
        {
            Name = "BtnOk",
            Content = "OK",
            Height = 20,
            Width = 60,
            HorizontalAlignment = HorizontalAlignment.Center,
            Background = Brushes.DarkGray,
            Foreground = Brushes.WhiteSmoke,
            Margin = new Thickness(0,0,0,5)
        };

   Border border = new Border();
        border.CornerRadius = new CornerRadius(3);

如何以编程方式在按钮中应用边框?

【问题讨论】:

  • 那里已经有一个了。默认模板中的父级是边框。只需在按钮上设置边框笔刷和粗细即可。

标签: c# wpf


【解决方案1】:

按钮不能应用边框。边框可以装饰按钮:

border.Child = button;

通常按钮在其模板 (ControlTemplate) 中已经有一个边框。那个 Border 不容易访问 - Button 类没有特殊属性,但是加载模板后可以在可视化树中找到该边框。

此外,如果您将边框放在 Button.Resources 中,则可以默认自定义样式。使用 Style.Setter 更改 CorderRadius:

var button = new System.Windows.Controls.Button
{
    Name = "BtnOk",
    Content = "OK",
    Height = 20,
    Width = 60,
    HorizontalAlignment = HorizontalAlignment.Center,
    Background = Brushes.DarkGray,
    Foreground = Brushes.WhiteSmoke,
    Margin = new Thickness(0, 0, 0, 5)
};

var style = new Style
{
    TargetType = typeof(Border),
    Setters = { new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(3) } }
};

button.Resources.Add(style.TargetType, style);

或使用对象/集合初始化器:

var button = new System.Windows.Controls.Button
{
    Name = "BtnOk",
    Content = "OK",
    Height = 20,
    Width = 60,
    HorizontalAlignment = HorizontalAlignment.Center,
    Background = Brushes.DarkGray,
    Foreground = Brushes.WhiteSmoke,
    Margin = new Thickness(0, 0, 0, 5),
    Resources =
    {
        {
            typeof(Border), new Style
            {
                TargetType = typeof(Border),
                Setters =
                {
                    new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(13) }
                }
            }
        }
    }
};

如果许多按钮应该具有不同的 CornerRadius,则更改按钮的模板可能是一种解决方案。更改模板并将 CornerRadius 设置为附加依赖属性,如本文所示:Set a property of a nested element in an WPF style

【讨论】:

    【解决方案2】:

    Button 的默认ControlTemplate 中确实有一个Border 元素,但是设置它的CornerRadius 属性的最简单方法是等到@ 987654325@ 已加载,然后获取对它的引用。试试这个:

    var button = new System.Windows.Controls.Button
    {
        Name = "BtnOk",
        Content = "OK",
        Height = 20,
        Width = 60,
        HorizontalAlignment = HorizontalAlignment.Center,
        Background = Brushes.DarkGray,
        Foreground = Brushes.WhiteSmoke,
        Margin = new Thickness(0, 0, 0, 5)
    };
    button.Loaded += (ss, ee) =>
    {
        Border border = button.Template.FindName("border", button) as Border;
        if (border != null)
            border.CornerRadius = new CornerRadius(3);
    };
    

    【讨论】:

    • @Suryakavitha:你试过这个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多