简体   繁体   English

如何以编程方式设置按钮样式(第 2 部分)?

[英]How to set Button Style programmatically (part 2)?

I'm trying to implement this answer (of how to round the corners of a Button), but programmatically.我正在尝试以编程方式实现这个答案(关于如何绕过按钮的角)。

I have:我有:

Style style = new Style(typeof(Button));
style.Setters.Add(new Setter(Border.CornerRadiusProperty, new CornerRadius(5)));
button.Style = style;

where button is a Button.其中button是一个按钮。 (Surprise!) (惊喜!)

Unfortunately, that doesn't do anything to the button...不幸的是,这对按钮没有任何作用......

Border.CornerRadiusProperty is a dependency property of Border, setting it on Button won't do anything. Border.CornerRadiusProperty是 Border 的一个依赖属性,在 Button 上设置它不会做任何事情。

If you place the edit cursor over your button in your window XAML, go to its property page, click on the little square to the right of "Miscellaneous -> Template" and select "Convert to new resource" then VS will expand out the template as a local resource for you to view and edit.如果将编辑光标放在窗口 XAML 中的按钮上,转到其属性页,单击“杂项 -> 模板”右侧的小方块并选择“转换为新资源”,然后 VS 将展开模板作为本地资源供您查看和编辑。 A glance at this template shows that while a border is present in the template, it only supports border brush and thickness:看一眼这个模板,虽然模板中存在边框,但它只支持边框刷和粗细:

<Border x:Name="border"
    BorderBrush="{TemplateBinding BorderBrush}"
    BorderThickness="{TemplateBinding BorderThickness}"
    Background="{TemplateBinding Background}"
    SnapsToDevicePixels="True">

So if you want to support corners on your buttons you'll need to modify this template and create a property for it to bind to which you can then set in your button declarations.因此,如果您想支持按钮上的角,您需要修改此模板并为其创建一个属性以绑定到该属性,然后您可以在按钮声明中设置该属性。 One way to do this is by sub-classing Button and adding a property such as BorderCornerRadius there.一种方法是通过子类化Button并在那里添加一个属性,例如BorderCornerRadius Another way is to create an attached property, which can then be applied to any framework element, including Button:另一种方法是创建一个附加属性,然后可以将其应用于任何框架元素,包括 Button:

public static class ButtonHelper
{
    public static CornerRadius GetBorderCornerRadius(DependencyObject obj)
    {
        return (CornerRadius)obj.GetValue(BorderCornerRadiusProperty);
    }

    public static void SetBorderCornerRadius(DependencyObject obj, CornerRadius value)
    {
        obj.SetValue(BorderCornerRadiusProperty, value);
    }

    public static readonly DependencyProperty BorderCornerRadiusProperty =
        DependencyProperty.RegisterAttached("BorderCornerRadius", typeof(CornerRadius), typeof(ButtonHelper), new PropertyMetadata(new CornerRadius(0)));
}

You can then modify the template to use this attached property to set the border corner radius:然后,您可以修改模板以使用此附加属性来设置边框角半径:

<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
        CornerRadius="{Binding (local:ButtonHelper.BorderCornerRadius), RelativeSource={RelativeSource TemplatedParent}}"
        ... etc ...

Now you can use this instead of Border.CornerRadius .现在您可以使用它代替Border.CornerRadius To do it in XAML you simply do this:要在 XAML 中执行此操作,您只需执行以下操作:

<Button x:Name="theButton" Width="100" Height="100"
        Template="{DynamicResource ButtonControlTemplate1}"
        local:ButtonHelper.BorderCornerRadius="20" />

Or you can do it in code-behind, as in your example code above (don't forget that you still need to override the control template):或者您可以在代码隐藏中执行此操作,如上面的示例代码中所示(不要忘记您仍然需要覆盖控件模板):

Style style = new Style(typeof(Button));
style.Setters.Add(new Setter(ButtonHelper.BorderCornerRadiusProperty, new CornerRadius(20)));
theButton.Style = style;

UPDATE: the answer you linked to works by changing the property for the default Border style, which you could achieve in code by doing this instead:更新:您链接到的答案通过更改默认Border样式的属性起作用,您可以通过执行以下操作在代码中实现:

Style style = new Style(typeof(Border));
style.Setters.Add(new Setter(Border.CornerRadiusProperty, new CornerRadius(20)));
theButton.Resources.Add(typeof(Border), style);

The problem with this is that it will be applied to all borders in that button, not just the button template but also any in the content or its DataTemplates etc. Sooner or later, as the complexity of your GUI increases, that might come back to haunt you.这样做的问题是它将应用于该按钮中的所有边框,不仅是按钮模板,还包括内容或其 DataTemplates 等中的任何边框。迟早,随着 GUI 的复杂性增加,这可能会回到困扰着你。 In general I find that while shortcuts like the above will get the job done in the short term, you'll have far fewer problems down the track by doing things properly.总的来说,我发现虽然像上面这样的捷径可以在短期内完成工作,但通过正确地做事,你在轨道上遇到的问题要少得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM