WPF的样式总的来说有两种使用方式
1.全局样式
1.1 在Windows.Resources下定义全局样式,当然Style作为一种资源,也可以在其他的地方定义资源(当没有X:KEY(扩展标记)值的时候就是对TargetType都有用,无语targetType绑定资源)
1 <Windows.Resources> 2 <Style x:Key="Buttonstyle2" TargetType="Button"> 3 <Setter Property="Background" Value="Coral"/> 4 <Setter Property="Template"> 5 <Setter.Value> 6 <ControlTemplate TargetType="{x:Type Button}"> 7 <Border Background="{TemplateBinding Background}" CornerRadius="5"> 8 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 9 </Border> 10 </ControlTemplate> 11 </Setter.Value> 12 </Setter> 13 <Style.Triggers> 14 <Trigger Property="IsMouseOver" Value="True"> 15 <Setter Property="Background" Value="Red"/> 16 </Trigger> 17 </Style.Triggers> 18 </Style> 19 20 </Windows.Resources>
1.2 控件引用资源
1 <Button Grid.Row="1" Content="123" Style="{StaticResource Buttonstyle2}" Margin="0,10,10,12" Width="60" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.517,-1.25"/>
特别说明:此样式作用时改变鼠标悬停的时候按钮颜色,以下代码是没有作用的,区别以上的ButtonStyle2
1 <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> 2 3 <Style.Triggers> 4 <Trigger Property="IsMouseOver" Value="True"> 5 <Setter Property="BorderBrush" Value="Black"></Setter> 6 <Setter Property="Background" Value="Green"></Setter> 7 <Setter Property="BorderThickness" Value="1"></Setter> 8 </Trigger> 9 <Trigger Property="IsMouseOver" Value="False"> 10 <Setter Property="BorderBrush" Value="Blue"></Setter> 11 <Setter Property="Background" Value="#FFFF2E00"></Setter> 12 <Setter Property="BorderThickness" Value="1"></Setter> 13 </Trigger> 14 15 </Style.Triggers> 16 </Style>
2.局部样式(也就是单独对一个控件有用)
1 <Button Height="30" Width="60" BorderBrush="{x:Null}" BorderThickness="1" Content="click" Grid.Row="2"> 2 <Button.Style> 3 <Style TargetType="Button"> 4 <Setter Property="Template"> 5 <Setter.Value> 6 <ControlTemplate TargetType="{x:Type Button}"> 7 <Border Background="{TemplateBinding Background}" CornerRadius="5"> 8 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 9 </Border> 10 </ControlTemplate> 11 </Setter.Value> 12 </Setter> 13 <Setter Property="Background" Value="#dd1144 "/> 14 <Style.Triggers> 15 <Trigger Property="IsPressed" Value="True"> 16 <Setter Property="Template"> 17 <Setter.Value> 18 <ControlTemplate TargetType="{x:Type Button}"> 19 <Border Background="#3f48cc" CornerRadius="4"> 20 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 21 </Border> 22 </ControlTemplate> 23 </Setter.Value> 24 </Setter> 25 </Trigger> 26 27 <Trigger Property="IsMouseOver" Value="True"> 28 <Setter Property="Template"> 29 <Setter.Value> 30 <ControlTemplate TargetType="{x:Type Button}"> 31 <Border Background="#3f48cc" > 32 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 33 </Border> 34 </ControlTemplate> 35 </Setter.Value> 36 </Setter> 37 </Trigger> 38 </Style.Triggers> 39 </Style> 40 </Button.Style> 41 </Button>
标签:11,10,13,12,样式,Basic,WPF From: https://www.cnblogs.com/HomeSapiens/p/17411014.html