【C#WPF】style
阿新 • • 發佈:2019-01-24
程式碼:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication4.MainWindow" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style x:Key="ButtonFocusVisual"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> </ControlTemplate> </Setter.Value> </Setter> </Style> <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="#F3F3F3" Offset="0"/> <GradientStop Color="#EBEBEB" Offset="0.5"/> <GradientStop Color="#DDDDDD" Offset="0.5"/> <GradientStop Color="#CDCDCD" Offset="1"/> </LinearGradientBrush> <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/> <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> <!--修改背景Background--> <!--"Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/--> <Setter Property="Background" Value="#FF8FC0E7"/> <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/> <!--不需要邊框,設定邊框寬度為0--> <!--Setter Property="BorderThickness" Value="1"/--> <Setter Property="BorderThickness" Value="0"/> <!--修改前景Foreground--> <!--Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/--> <Setter Property="Foreground" Value="White"/> <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}"> <!--將Themes:ButtonChrome修改為Border控制元件--> <!--Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true"> <ContentPresenter HorizontalAlignment="Left" Margin="16.057,1,0,1" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Themes:ButtonChrome--> <Border BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true" CornerRadius="5"> <Grid Height="33"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> <ContentPresenter Grid.Column="1" HorizontalAlignment="Center" Margin="5.057,8.88,0,8.88" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> <Path Grid.Column="0" Data="M5,15 L17.5,15" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="2.5" Margin="5,13.5,0,0" Stretch="Fill" Stroke="White" VerticalAlignment="Top" Width="13.5" StrokeThickness="5"/> <Path Grid.Column="0" Data="M11,7 L11,21.5" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="15.5" Margin="10.5,7,0,0" Stretch="Fill" Stroke="White" VerticalAlignment="Top" Width="1.5" StrokeThickness="5"/> </Grid> </Border> <!--取消原生的Themes:ButtonChrome觸發器效果,如需要一些選擇效果,根據自己需要加上--> <!--ControlTemplate.Triggers> <Trigger Property="IsKeyboardFocused" Value="true"> <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/> </Trigger> <Trigger Property="ToggleButton.IsChecked" Value="true"> <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="#ADADAD"/> </Trigger> </ControlTemplate.Triggers--> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Button Content="好友" HorizontalAlignment="Left" Margin="223,123,0,0" VerticalAlignment="Top" Width="75" Style="{DynamicResource ButtonStyle1}" /> <Button Content="Button" HorizontalAlignment="Left" Margin="56,123,0,0" VerticalAlignment="Top" Width="75" Height="33"/> </Grid> </Window>