C# 在按钮模板上设置拐角半径
我想有一个按钮来定义noC# 在按钮模板上设置拐角半径,c#,wpf,silverlight,xaml,C#,Wpf,Silverlight,Xaml,我想有一个按钮来定义noCornerRadius和另外两个按钮,我怎样才能做到这一点 <Style TargetType="Button" x:Key="TabButton"> <Setter Property="Background" Value="White" /> <Setter Property="TextBlock.TextAlignment" Value="Center" /> <Setter Property="Te
CornerRadius
和另外两个按钮,我怎样才能做到这一点
<Style TargetType="Button" x:Key="TabButton">
<Setter Property="Background" Value="White" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="0" Background="White" BorderBrush="#ccc" BorderThickness="0,1,1,0" >
<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>
<Style TargetType="Button" x:Key="TabButtonFirst" BasedOn="{StaticResource TabButton}">
<Setter Property="CornerRadius" Value="3,0,0,0" />
</Style>
<Style TargetType="Button" x:Key="TabButtonLast" BasedOn="{StaticResource TabButton}">
<Setter Property="CornerRadius" Value="0,0,0,3" />
</Style>
正如Nitesh所说,按钮上没有CornerRadius属性,它是边框的属性,如您在第一个样式中所示,只需复制第一个样式并更改CornerRadius,然后将其指定给相应按钮的样式即可
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button" x:Key="TabButton">
<Setter Property="Background" Value="White" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="0" Background="White" BorderBrush="#ccc" BorderThickness="0,1,1,0" >
<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>
<Style TargetType="Button" x:Key="TabButtonFirst">
<Setter Property="Background" Value="White" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="3,0,0,0" Background="White" BorderBrush="#ccc" BorderThickness="0,1,1,0" >
<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>
<Style TargetType="Button" x:Key="TabButtonLast">
<Setter Property="Background" Value="White" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="0,0,0,3" Background="White" BorderBrush="#ccc" BorderThickness="0,1,1,0" >
<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>
</Window.Resources>
<Grid Background="Black">
<Button Style="{StaticResource TabButton}" Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,72,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<Button Style="{StaticResource TabButtonFirst}" Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,43,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
<Button Style="{StaticResource TabButtonLast}" Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,101,0,0" Name="button3" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
您不局限于正在模板化的控件的依赖项属性。在这种情况下,
按钮
没有CornerRadius
属性,而Border
有属性,因此您可以使用Border.CornerRadius
:
<Style TargetType="Button" x:Key="TabButton">
<Setter Property="Background" Value="White" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="{TemplateBinding Border.CornerRadius}"
Background="White" BorderBrush="#ccc"
BorderThickness="0,1,1,0" >
<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>
<Style TargetType="Button" x:Key="TabButtonFirst" BasedOn="{StaticResource TabButton}">
<Setter Property="Border.CornerRadius" Value="3,0,0,0" />
</Style>
<Style TargetType="Button" x:Key="TabButtonLast" BasedOn="{StaticResource TabButton}">
<Setter Property="Border.CornerRadius" Value="0,0,0,3" />
</Style>
使用这种方法,您不再需要维护控件模板的多个副本。我将创建自己的自定义按钮类(从button继承),其中包括CornerRadius依赖属性。然后样式的目标类型成为这个新类,您可以使用模板绑定来设置角半径 使用这种方法,不仅不需要维护控件模板的多个副本,而且不需要每次修改角半径时都创建新样式。您可以直接设置或绑定到CornerRadius依赖项属性 因此,控件的代码可能如下所示:
public class MyCustomButton : Button
{
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(MyCustomButton), new FrameworkPropertyMetadata(new CornerRadius(0)));
public CornerRadius CornerRadius
{
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
}
<!--Button-->
<Button
Name="myButton"
Content="OK"
FontFamily="Century Gothic"
Foreground="white"
Background="CornflowerBlue"
BorderThickness="0"
Padding="10"
Margin="10,5">
<Button.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="7"/>
</Style>
</Button.Resources>
</Button>
以及XAML:
<Style TargetType="MyCustomButton" x:Key="TabButton">
<Setter Property="Background" Value="White" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MyCustomButton">
<Border CornerRadius="{TemplateBinding CornerRadius}" Background="White" BorderBrush="#ccc" BorderThickness="0,1,1,0" >
<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>
因此,对于不需要角半径的按钮,由于dependency属性默认为0,因此无需执行任何操作。对于具有角半径的按钮,只需将dependency属性设置为适当的值,就像普通边框的CornerRadius属性一样。只需创建如下新按钮:
public class MyCustomButton : Button
{
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(MyCustomButton), new FrameworkPropertyMetadata(new CornerRadius(0)));
public CornerRadius CornerRadius
{
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
}
<!--Button-->
<Button
Name="myButton"
Content="OK"
FontFamily="Century Gothic"
Foreground="white"
Background="CornflowerBlue"
BorderThickness="0"
Padding="10"
Margin="10,5">
<Button.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="7"/>
</Style>
</Button.Resources>
</Button>
您可以使用附加属性设置按钮边框半径(同样适用于文本框) 为附加属性创建类
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;
}
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);
}
}
然后,可以使用附加属性语法设置多个按钮的样式,而不使用重复样式:
<Button local:CornerRadiusSetter.CornerRadius="3,0,0,0">Click me!</Button>
<Button local:CornerRadiusSetter.CornerRadius="0,0,0,3">Click me!</Button>
点击我!
点击我!
不使用控制模板
您只需使用样式。参考资料
并以边框为目标:
<Style TargetType="Button" x:Key="TabButton">
<Setter Property="Background" Value="White" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Style.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="3,0,0,0" />
</Style>
</Style.Resources>
</Style>
private Button[]Buttonsd()//选择页面上的所有按钮
{
///将内容投射到面板中
Panel mainContainer=(Panel)this.Content;
///GetAll元素
UIElementCollection元素=mainContainer.Children;
///将UIElementCollection强制转换为列表
///
List lstElement=element.Cast().ToList();
///从列表中获取所有控件
///
按钮[]btns=lstElement.OfType().ToArray();
返回基站;
//添加(新按钮(){Content=“HELLOMOTO”});添加了新控件
}
专用空心半径按钮(int radiusvalue,按钮btn)
{
ControlTemplate=btn.template;
Border Border=(Border)template.FindName(“Border”,btn);
拐角半径=新拐角半径(半径值);
Border.CornerRadius=半径;
}
私有void allbuttonsettings()
{
Button[]buttons=Buttonsd();//创建按钮集合
foreach(Buttonsd()中的按钮btn)
{
半径按钮(21,btn);
}
}
按钮没有CornerRadius属性。在ControlTemplate中为您的边框控件设置它。您需要两种样式的按钮来实现您正在执行的操作,或者创建一个自定义按钮来实现CornerRadius作为DependencyProperty,并将其与ControlTemplate中的CornerRadius Border绑定。功能正常,但按钮失去其默认样式,即“OnMouseOver”,“点击鼠标“事件不会改变按钮的视觉状态。每当我有两个模板,只有一个微小的变化时,我就会遇到这种情况。这种重复的代码让我很烦恼。一定有更好的办法。@Jordan:有,看我的答案。:)虽然这样做有效,但一点也不干燥。除了琐碎的样式之外,任何东西都会很快变得难以管理。如果我想要一个圆形按钮,会发生什么?我必须使转弯半径至少为按钮大小的一半。我怎样才能确保按钮不会变大到超过拐角半径呢?答案太棒了!我知道我们不喜欢+1评论,但这是大多数人在导致他们提出这个问题的情况下应该寻找的真正答案。@Pieter Witvoet:我如何约束Co的价值