-1

我的按钮中有样式资源以使其圆润,并且我想使用 c#(后面的代码)创建它,我该怎么做?

<Button.Resources>
    <Style TargetType="Border">
        <Setter Property="CornerRadius" Value="5"/>
    </Style>
</Button.Resources>
4

1 回答 1

2
  1. 创建Style

     Style style = new Style() { TargetType = typeof(Border) };
     style.Setters.Add(new Setter() { Property = Border.CornerRadiusProperty, Value = new CornerRadius(5) });
     style.Seal();
    
  2. 将其添加到Button

     button.Resources.Add(typeof(Border), style);
    

XAML:

<Button x:Name="button" Content="..." />
于 2021-08-17T18:21:41.240 回答