定制控件结构(controltemplate)
在app.xaml页面中写样式,形式如下代码:
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightApplication2.App">
<Application.Resources>
<Style TargetType="Button" x:Key="RoundButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="Root">
<Ellipse Width="240" Height="120">
<Ellipse.Fill>
<RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5">
<GradientStop Offset="0.2" Color="#FFFFFF"/>
<GradientStop Offset="1" Color="#EC04FA"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="提交" FontSize="26" Foreground="Black" HorizontalAlignment="Center"></TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
在布局页面这么写:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightApplication2.Page"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White">
<Button Height="120" Width="120" Margin="236,0,277,123" VerticalAlignment="Bottom" Content="按钮" x:Name="btn" Click="btn_Click" Style="{StaticResource RoundButton}"/>
</Grid>
</UserControl>
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightApplication2.App">
<Application.Resources>
<Style TargetType="Button" x:Key="RoundButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="Root">
<Ellipse Width="240" Height="120">
<Ellipse.Fill>
<RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5">
<GradientStop Offset="0.2" Color="#FFFFFF"/>
<GradientStop Offset="1" Color="#EC04FA"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="提交" FontSize="26" Foreground="Black" HorizontalAlignment="Center"></TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
在布局页面这么写:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightApplication2.Page"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White">
<Button Height="120" Width="120" Margin="236,0,277,123" VerticalAlignment="Bottom" Content="按钮" x:Name="btn" Click="btn_Click" Style="{StaticResource RoundButton}"/>
</Grid>
</UserControl>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步