Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用C以编程方式将边框应用于按钮控件#_C#_Wpf - Fatal编程技术网

C# 如何使用C以编程方式将边框应用于按钮控件#

C# 如何使用C以编程方式将边框应用于按钮控件#,c#,wpf,C#,Wpf,我正在使用C#创建一个按钮控件,如下代码所述。我已经为按钮样式创建了圆形边框。我无法在按钮中看到任何要指定边框的属性 var button = new System.Windows.Controls.Button { Name = "BtnOk", Content = "OK", Height = 20, Width = 60, HorizontalAli

我正在使用C#创建一个按钮控件,如下代码所述。我已经为按钮样式创建了圆形边框。我无法在按钮中看到任何要指定边框的属性

   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);

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

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

border.Child = button;

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

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

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设置为附加的依赖项属性,如本文所示:

按钮无法应用边框。边框可以装饰按钮:

border.Child = button;

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

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

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设置为附加的依赖项属性,如本文所示:

默认的
控件模板中确实有一个
Border
元素,用于
按钮
,但这是设置其
CornerRadius
属性的最简单方法,无需定义自定义模板,等待
按钮
加载完毕,然后获取对它的引用。试试这个:

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);
};

对于
按钮
,默认的
控制模板
中确实有一个
边框
元素,但是设置按钮的
拐角半径
属性最简单的方法是,不必定义自定义模板,等待按钮
加载完毕,然后获取对它的引用。试试这个:

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:你试过吗?@Suryakavitha:你试过吗?