【问题标题】:Create Round Button with Border IN UWP Windows 10 C#在 UWP Windows 10 C# 中创建带边框的圆形按钮
【发布时间】:2016-09-11 11:34:57
【问题描述】:

我正在尝试在 UWP Windows 10 中创建一个带有白色边框和透明背景的圆形按钮(如 Windows 8.1 中的旧 AppBarButtons)。

我找到了几个这样的示例:

https://comentsys.wordpress.com/2015/05/23/windows-10-universal-windows-platform-custom-button/

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/cda7a526-5e99-4d4a-a73c-0be4ce77f961/uwpwindows10-how-to-make-button-with-round-edges?forum=wpdevelop&prof=required

但问题在于边界。 当我将 BorderBrush 设置为某种颜色时,事实证明 Border 是 Button 的“矩形”。

有没有办法为按钮创建圆形边框?

【问题讨论】:

  • 您可以在此处投票支持对圆形按钮的开箱即用支持:UserVoice: Add CornerRadius property to Button
  • 似乎它在 1809 Oct 2018 build 中可用
  • @peterincumbria 如果您安装了 1809 套件,则可以在早期的目标版本中使用它,方法是将其用作 Button 的替换属性:Windows10version1809:CornerRadius="90,90,90,90"

标签: c# xaml button win-universal-app


【解决方案1】:

对于1809 及更高版本(即使您在下面针对像我这样的早期版本),您只需以圆形按钮为例:

<Button
    Content="Stuff"
    Windows10version1809:CornerRadius="90,90,90,90"
    Height="64"
    Width="64">
</Button>

瞧!

确保将其与其他 xmlns 内容一起添加到 XAML 的顶部。

xmlns:Windows10version1809="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract, 7)"

【讨论】:

    【解决方案2】:

    我不知道 OP 是否仍然对圆角按钮感兴趣,但为了完整起见,我认为其他用户可能会有所帮助,正如用户 peterincumbria 所说,对于 Windows 的最新版本10 (1809) 在Control 类中有一个新属性:CornerRadius。所以现在下面的代码就足够了:

    <Button Content="DEMO"
            Background="Transparent"                    
            BorderThickness="1.0"
            BorderBrush="White"
            CornerRadius="10"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"/>
    

    把它放在Grid 中,我们在Page 中间有一个圆角按钮。

    【讨论】:

      【解决方案3】:

      另一个样本

      <Style x:Key="ProfileButtonStyle" TargetType="Button">
              <Setter Property="Background" Value="{StaticResource AppBarHeaderBackground}"/>
              <Setter Property="BorderBrush" Value="Transparent"/>
              <Setter Property="FontFamily" Value="Segoe UI"/>
              <Setter Property="Foreground" Value="{StaticResource SystemControlBackgroundAccentBrush}"></Setter>
              <Setter Property="FontSize" Value="9"/>
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="Button">
                          <Grid>
                              <StackPanel Orientation="Vertical">
                                  <Grid  Margin="0,0,0,0" >
                                      <Ellipse x:Name="ButtonShape" Height="40" Width="40" Fill="#FFFDFCFC" HorizontalAlignment="Center"
                                          Stroke="{StaticResource SystemControlBackgroundAccentBrush}" StrokeThickness="2" VerticalAlignment="Center"/>
                                      <!--This glyph is the Contact (head and shoulders silhouette) glyph. -->
                                      <TextBlock x:Name="Icon" Text="&#xE13D;" FontFamily="Segoe UI Symbol" FontSize="18" HorizontalAlignment="Center"
                                          VerticalAlignment="Center"/>
                                  </Grid>
                                  <TextBlock x:Name="ButtonContent" Text="{TemplateBinding Content}" HorizontalAlignment="Center"
                                      FontFamily="Segoe UI" FontSize="12"/>
                              </StackPanel>
                              <VisualStateManager.VisualStateGroups>
                                  <VisualStateGroup x:Name="CommonStates">
                                      <VisualState x:Name="Normal"/>
                                      <VisualState x:Name="PointerOver">
                                          <Storyboard>
                                              <ColorAnimation Duration="0" To="{Binding Source={StaticResource SymbolThemeFontFamily}, Path=Color}" 
                                                              Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" 
                                                              Storyboard.TargetName="ButtonShape" />
                                          </Storyboard>
                                      </VisualState>
                                      <VisualState x:Name="ButtonPressed">
                                          <Storyboard>
                                              <ColorAnimation Duration="0" To="#eeeeee" 
                                                              Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" 
                                                              Storyboard.TargetName="ButtonEllipse" />
                                          </Storyboard>
                                      </VisualState>
                                  </VisualStateGroup>
                              </VisualStateManager.VisualStateGroups>
                          </Grid>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      

      结果

      或者创建完全可定制的圆角半径

      //创建模板控件xaml设计

      <ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:UWP.Library2.Controls">
      <Style  TargetType="local:CustomRoundedButton">
          <Setter Property="CornerRadius" Value="10,10,10,10"></Setter>
          <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
          <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
          <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundTransparentBrush}"/>
          <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>
          <Setter Property="Padding" Value="8,4,8,4"/>
          <Setter Property="HorizontalAlignment" Value="Left"/>
          <Setter Property="VerticalAlignment" Value="Center"/>
          <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
          <Setter Property="FontWeight" Value="Normal"/>
          <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
          <Setter Property="UseSystemFocusVisuals" Value="True"/>
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="local:CustomRoundedButton">
                      <Grid x:Name="RootGrid" Background="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" CornerRadius="{TemplateBinding CornerRadius}">
                          <VisualStateManager.VisualStateGroups>
                              <VisualStateGroup x:Name="CommonStates">
                                  <VisualState x:Name="Normal">
                                      <Storyboard>
                                          <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/>
                                      </Storyboard>
                                  </VisualState>
                                  <VisualState x:Name="PointerOver">
                                      <Storyboard>
                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}"/>
                                          </ObjectAnimationUsingKeyFrames>
                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
                                          </ObjectAnimationUsingKeyFrames>
                                          <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/>
                                      </Storyboard>
                                  </VisualState>
                                  <VisualState x:Name="Pressed">
                                      <Storyboard>
                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}"/>
                                          </ObjectAnimationUsingKeyFrames>
                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}"/>
                                          </ObjectAnimationUsingKeyFrames>
                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
                                          </ObjectAnimationUsingKeyFrames>
                                          <PointerDownThemeAnimation Storyboard.TargetName="RootGrid"/>
                                      </Storyboard>
                                  </VisualState>
                                  <VisualState x:Name="Disabled">
                                      <Storyboard>
                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
                                          </ObjectAnimationUsingKeyFrames>
                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}"/>
                                          </ObjectAnimationUsingKeyFrames>
                                          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}"/>
                                          </ObjectAnimationUsingKeyFrames>
                                      </Storyboard>
                                  </VisualState>
                              </VisualStateGroup>
                          </VisualStateManager.VisualStateGroups>
                          <!--<Rectangle RadiusX="60" RadiusY="60" Fill="{TemplateBinding Background}" Margin="0,0,10,0" />-->
                          <Grid Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding CornerRadius}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" ></Grid>
                          <ContentPresenter  CornerRadius="{TemplateBinding CornerRadius}" x:Name="ContentPresenter" 
                                             AutomationProperties.AccessibilityView="Raw" BorderBrush="{TemplateBinding BorderBrush}" 
                                             BorderThickness="{TemplateBinding BorderThickness}" 
                                             ContentTemplate="{TemplateBinding ContentTemplate}"
                                             ContentTransitions="{TemplateBinding ContentTransitions}" 
                                             Content="{TemplateBinding Content}" 
                                             HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                             Padding="{TemplateBinding Padding}" 
                                             VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                      </Grid>
                  </ControlTemplate>
              </Setter.Value>
          </Setter>
      </Style>
      

      //模板控件.cs

      public sealed class CustomRoundedButton : Button
      {
          private Grid _rootGrid = null;
          public CustomRoundedButton()
          {
              this.DefaultStyleKey = typeof(CustomRoundedButton);
          }
          protected override void OnApplyTemplate()
          {
              base.OnApplyTemplate();
              _rootGrid = GetTemplateChild("RootGrid") as Grid;
          }
          public CornerRadius CornerRadius
          {
              get { return (CornerRadius)GetValue(CornerRadiusProperty); }
              set { SetValue(CornerRadiusProperty, value); }
          }
          public static readonly DependencyProperty CornerRadiusProperty =
              DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(CustomRoundedButton), new PropertyMetadata(new CornerRadius(10,10,10,10)));
      
      }
      

      //从/Themes/Generic.xaml注册或合并字典

      <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="ms-appx:///UWP.Library2/Controls/CustomRoundedButton/CustomRoundedButton.xaml" />
            </ResourceDictionary.MergedDictionaries>
      

      【讨论】:

        【解决方案4】:

        我刚才使用的方法是简单地将 CornerRadius 添加到 Button 模板中的“RootGrid”网格中。

        <Style TargetType="Button" x:Key="RoundedButton">
            ...
            <Grid x:Name="RootGrid" CornerRadius="10" Background="{TemplateBinding Background}"> 
            ...
        </Style>
        

        【讨论】:

          【解决方案5】:

          有几种方法可以实现这一点,一种使用样式可以看起来像这样 - 从 ContentPresenter 中删除 BorderBrush 并添加一个 Ellipse用那把刷子。 XAML 中的示例:

          <Page.Resources>
              <Style x:Key="CircleButtonStyle" TargetType="Button">
                  <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
                  <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
                  <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundTransparentBrush}"/>
                  <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>
                  <Setter Property="Padding" Value="8,4,8,4"/>
                  <Setter Property="HorizontalAlignment" Value="Left"/>
                  <Setter Property="VerticalAlignment" Value="Center"/>
                  <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
                  <Setter Property="FontWeight" Value="Normal"/>
                  <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
                  <Setter Property="UseSystemFocusVisuals" Value="True"/>
                  <Setter Property="Template">
                      <Setter.Value>
                          <ControlTemplate TargetType="Button">
                              <Grid x:Name="RootGrid">
                                 <VisualStateManager.VisualStateGroups>
                                      <VisualStateGroup x:Name="CommonStates">
                                          <VisualState x:Name="Normal">
                                              <Storyboard>
                                                  <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="PointerOver">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="BorderCircle">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="Pressed">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="BorderCircle">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <PointerDownThemeAnimation Storyboard.TargetName="RootGrid"/>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="Disabled">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="BorderCircle">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="BorderCircle">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                      </VisualStateGroup>
                                  </VisualStateManager.VisualStateGroups>
                                  <ContentPresenter x:Name="ContentPresenter" VerticalAlignment="Center" AutomationProperties.AccessibilityView="Raw" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                  <Ellipse x:Name="BorderCircle" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="2"/>
                              </Grid>
                          </ControlTemplate>
                      </Setter.Value>
                  </Setter>
              </Style>
          </Page.Resources>
          
          <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
              <Button Content="text" Width="50" Height="50" BorderBrush="Blue" Style="{StaticResource CircleButtonStyle}"/>
          </Grid>
          

          我还在 VisualStates 中进行了一些更改,以便在单击/禁用后看起来不会很奇怪。

          【讨论】:

          • 这会引发 xaml 异常:“无法解析 TargetName BorderCircle”
          • 应该是&lt;Ellipse Name="BorderCircle" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="2"/&gt;
          • @TekGiant 对,我在复制过程中不知何故丢失了这个。谢谢。
          【解决方案6】:

          你在寻找这样的东西吗?

          <StackPanel>
              <Button Background="Transparent">
                  <StackPanel>
                      <Border CornerRadius="10" 
                              Background="Transparent" 
                              BorderBrush="White" 
                              BorderThickness="3">
                          <TextBlock Text="MyButton" 
                                     Margin="10" 
                                     Foreground="White"/>
                      </Border>
                  </StackPanel>
              </Button>
          </StackPanel>
          

          【讨论】:

          • 唯一的缺点是如果数字少于两位数,圆圈的大小会有所不同......
          • @Cabuxa.Mapache 你可以把按钮变大
          猜你喜欢
          • 2021-03-04
          • 2018-10-04
          • 2018-10-09
          • 1970-01-01
          • 1970-01-01
          • 2015-09-28
          • 2014-12-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多