visual studio redondos redondo redondeados redondeado para net formulario form estilos con botones boton bordes .net wpf visual-studio wpf-controls styles

.net - studio - ¿Cómo crear/hacer botones de esquina redondeados en WPF?



formulario con bordes redondeados c# (9)

Necesito crear un botón brillante de esquina redondeada en WPF. ¿Alguien puede por favor explicarme qué pasos son necesarios?


Bueno, la mejor forma de esquivar las esquinas rápidamente y con una animación estándar es crear una copia de la plantilla de control con Blend. Una vez que obtenga una copia, establezca el radio de la esquina en la etiqueta de cuadrícula y podrá tener su control con la funcionalidad de animación completa y aplicable a cualquier control de botón. mira este es el código:

<ControlTemplate x:Key="ButtonControlTemplate" TargetType="Button"> <Grid x:Name="RootGrid" Background="{TemplateBinding Background}" CornerRadius="8,8,8,8"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"> <Storyboard> <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" /> </Storyboard> </VisualState> <VisualState x:Name="PointerOver"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPointerOver}" /> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPressed}" /> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPointerOver}" /> </ObjectAnimationUsingKeyFrames> <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" /> </Storyboard> </VisualState> <VisualState x:Name="Pressed"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPressed}" /> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPressed}" /> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPressed}" /> </ObjectAnimationUsingKeyFrames> <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" /> </Storyboard> </VisualState> <VisualState x:Name="Disabled"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundDisabled}" /> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushDisabled}" /> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundDisabled}" /> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <!--<Border CornerRadius="8,8,8,8" Background="#002060" BorderBrush="Red" BorderThickness="2">--> <ContentPresenter x:Name="ContentPresenter" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" ContentTemplate="{TemplateBinding ContentTemplate}" Padding="{TemplateBinding Padding}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" AutomationProperties.AccessibilityView="Raw"/> <!--</Border>--> </Grid> </ControlTemplate>

También edité el VisualState = "PointerOver" específicamente en Storyboard.TargetName = "BorderBrush", porque su ThemeResource obtiene esquinas al cuadrado cuando se activa PointerOver.

Entonces debería poder aplicarlo a su estilo de control de esta manera:

<Style TargetType="ContentControl" x:Key="ButtonLoginStyle" BasedOn="{StaticResource CommonLoginStyleMobile}"> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="Background" Value="#002060"/> <Setter Property="Template" Value="{StaticResource ButtonControlTemplate}"/> </Style>

Entonces puedes aplicar tus estilos a cualquier botón.


Como alternativa, puede codificar algo como esto:

<Border x:Name="borderBtnAdd" BorderThickness="1" BorderBrush="DarkGray" CornerRadius="360" Height="30" Margin="0,10,10,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="30"> <Image x:Name="btnAdd" Width="{Binding borderBtnAdd.Width}" Height="{Binding borderBtnAdd.Height}" Source="Recursos/Images/ic_add_circle_outline_black_24dp_2x.png"/> </Border>

El "Botón" se verá más o menos así:

Podrías configurar cualquier otro contenido en lugar de la imagen.


Esta es una versión adaptada de la respuesta de @Kishore Kumar que es más simple y más cercana al estilo y los colores predeterminados de los botones. También soluciona el problema de que su disparador "IsPressed" está en el orden incorrecto y nunca se ejecutará, ya que el "MouseOver" tendrá precedentes:

<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid x:Name="grid"> <Border x:Name="border" CornerRadius="2" BorderBrush="#707070" BorderThickness="1" Background="LightGray"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" TextElement.FontWeight="Normal"> </ContentPresenter> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" TargetName="border" Value="#BEE6FD"/> <Setter Property="BorderBrush" TargetName="border" Value="#3C7FB1"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="BorderBrush" TargetName="border" Value="#2C628B"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" TargetName="grid" Value="0.25"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter>


Esto es más de una plantilla de control mínima para obtener un botón con esquinas redondeadas, sin embargo, no tendrá ningún desplazamiento visual o haga clic en los efectos visuales. Pero puede agregarlos a la plantilla de control según sea necesario. Estaba trabajando con un fondo oscuro, de ahí el fondo blanco.

<Style x:Key="RoundedButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border CornerRadius="15" Background="White" BorderThickness="1" Padding="2"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>

Utilicé la plantilla de control de la siguiente publicación de blog como mi punto de partida: http://shihac-sharp.blogspot.com.au/2012/05/button-with-rounded-corners-in-wpf.html


Puede usar propiedades adjuntas para configurar el radio del borde del botón (también lo mismo funcionará para los cuadros de texto).

Crear clase para la propiedad adjunta

public class CornerRadiusSetter { public static CornerRadius GetCornerRadius(DependencyObject obj) => (CornerRadius)obj.GetValue(CornerRadiusProperty); public static void SetCornerRadius(DependencyObject obj, CornerRadius value) => obj.SetValue(CornerRadiusProperty, value); public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached(nameof(Border.CornerRadius), typeof(CornerRadius), typeof(CornerRadiusSetter), new UIPropertyMetadata(new CornerRadius(), CornerRadiusChangedCallback)); public static void CornerRadiusChangedCallback(object sender, DependencyPropertyChangedEventArgs e) { Control control = sender as Control; if (control == null) return; control.Loaded -= Control_Loaded; control.Loaded += Control_Loaded; } private static void Control_Loaded(object sender, EventArgs e) { Control control = sender as Control; if (control == null || control.Template == null) return; control.ApplyTemplate(); Border border = control.Template.FindName("border", control) as Border; if (border == null) return; border.CornerRadius = GetCornerRadius(control); } }

Luego puede usar la sintaxis de propiedad adjunta para varios botones sin duplicados de estilo:

<Button local:CornerRadiusSetter.CornerRadius="10">Click me!</Button> <Button local:CornerRadiusSetter.CornerRadius="5, 0, 0, 5">Click me!</Button> <Button local:CornerRadiusSetter.CornerRadius="3, 20, 8, 15">Click me!</Button>


Puedes probar esto ...........

<Border BorderBrush="Black" Name="MyBorder" Height="78" Background="Red" Width="74" CornerRadius="3"> <Button Width="{Binding MyBorder.Width}" Height="{Binding MyBorder.Height}" Content="Hi" Background="Red"/> </Border>


Tienes que crear tu propia Plantilla de Control para el Botón. solo eche un vistazo a la muestra

Creé un estilo llamado RoundCorner y, en mi interior, cambié, en lugar de eso, creé mi propia nueva Plantilla de Control con Borde (CornerRadius = 8) para la esquina redonda y algún fondo y otro efecto de activación. Si tiene o sabe Expression Blend, puede hacerlo muy fácilmente.

<Style x:Key="RoundCorner" TargetType="{x:Type Button}"> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Padding" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid x:Name="grid"> <Border x:Name="border" CornerRadius="8" BorderBrush="Black" BorderThickness="2"> <Border.Background> <RadialGradientBrush GradientOrigin="0.496,1.052"> <RadialGradientBrush.RelativeTransform> <TransformGroup> <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.5" ScaleY="1.5"/> <TranslateTransform X="0.02" Y="0.3"/> </TransformGroup> </RadialGradientBrush.RelativeTransform> <GradientStop Offset="1" Color="#00000000"/> <GradientStop Offset="0.3" Color="#FFFFFFFF"/> </RadialGradientBrush> </Border.Background> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" TextElement.FontWeight="Bold"> </ContentPresenter> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" TargetName="border"> <Setter.Value> <RadialGradientBrush GradientOrigin="0.496,1.052"> <RadialGradientBrush.RelativeTransform> <TransformGroup> <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.5" ScaleY="1.5"/> <TranslateTransform X="0.02" Y="0.3"/> </TransformGroup> </RadialGradientBrush.RelativeTransform> <GradientStop Color="#00000000" Offset="1"/> <GradientStop Color="#FF303030" Offset="0.3"/> </RadialGradientBrush> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" TargetName="border" Value="#FF33962B"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" TargetName="grid" Value="0.25"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>

Utilizando

<Button Style="{DynamicResource RoundCorner}" Height="25" VerticalAlignment="Top" Content="Show" Width="100" Margin="5" />


en su app.xaml agregue esta parte del estilo:

<Application.Resources> <Style TargetType="FrameworkElement" x:Key="VisibleAnimation"> <Setter Property="Visibility" Value="Collapsed"/> <Setter Property="Opacity" Value="10"/> <Setter Property="Height" Value="700"></Setter> <Style.Triggers> <Trigger Property="Visibility" Value="Visible"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.0" To="1.0" Duration="0:0:0.5"/> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> </Trigger> </Style.Triggers> </Style> <Style TargetType="Button" x:Key="BTNCORNER"> <Setter Property="Background" Value="White" /> <Setter Property="TextBlock.TextAlignment" Value="Center" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border CornerRadius="7,7,7,7" Background="White" BorderBrush="#ccc" BorderThickness="1,1,1,1" > <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Application.Resources> then go to your button <Button x:Name="loginButton" Style="{StaticResource BTNCORNER}" Margin="50,20,20,20" Click="loginButton_Click" FontSize="20" Width="93" Height="42" />


<Button x:Name="btnBack" Grid.Row="2" Width="300" Click="btnBack_Click"> <Button.Template> <ControlTemplate> <Border CornerRadius="10" Background="#463190"> <TextBlock Text="Retry" Foreground="White" HorizontalAlignment="Center" Margin="0,5,0,0" Height="40" FontSize="20"></TextBlock> </Border> </ControlTemplate> </Button.Template> </Button>

Esto está funcionando bien para mí.