WPFのStyle TargetType的不同写法

一、隐式写法

<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="28"/>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="28"/>
</Style>

二、显示写法

<Style x:Key="cc" TargetType="{x:Type TextBlock}">
      <Setter Property="FontSize" Value="28"/>
</Style>

三、自动应用写法

<Style x:Key="{x:Type TextBlock}" TargetType="TextBlock">
   <Setter Property="FontSize" Value="28"/>
</Style>

四、内部写法(写在内部,“.”分割)

<Style x:Key="aa">
  <Setter Property="Control.FontSize" Value="28"/>
</Style>

 

转载于:https://www.cnblogs.com/xietianjiao/p/11077223.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 WPF ComboBox 样式示例: ```xml <Window.Resources> <Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}"> <Setter Property="Foreground" Value="Black"/> <Setter Property="Background" Value="White"/> <Setter Property="FontSize" Value="14"/> <Setter Property="Padding" Value="5,2"/> <Setter Property="Height" Value="30"/> <Setter Property="Width" Value="120"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBox}"> <Grid> <ToggleButton x:Name="ToggleButton" BorderBrush="Gray" BorderThickness="1" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" > <ToggleButton.Template> <ControlTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="20" /> </Grid.ColumnDefinitions> <Border x:Name="Border" Background="{TemplateBinding Background}" BorderThickness="0" CornerRadius="0" Grid.ColumnSpan="2" /> <Path x:Name="Arrow" Grid.Column="1" Fill="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="Border" Property="Background" Value="#EEE" /> <Setter TargetName="Border" Property="BorderBrush" Value="#AAA" /> <Setter Property="Foreground" Value="#AAA"/> <Setter TargetName="Arrow" Property="Fill" Value="#AAA" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Border" Property="Background" Value="#DDD" /> </Trigger> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="Border" Property="Background" Value="#EEE" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </ToggleButton.Template> </ToggleButton> <Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide"> <Grid x:Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}"> <Border x:Name="DropDownBorder" Background="White" BorderThickness="1" BorderBrush="Gray"/> <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> <ItemsPresenter SnapsToDevicePixels="True" /> </ScrollViewer> </Grid> <Popup.Style> <Style TargetType="{x:Type Popup}"> <Style.Triggers> <Trigger Property="HasDropShadow" Value="True"> <Setter Property="Margin" Value="0,0,5,5" /> </Trigger> </Style.Triggers> </Style> </Popup.Style> </Popup> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <StackPanel> <ComboBox Style="{StaticResource ComboBoxStyle}" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" /> </StackPanel> ``` 这个样式将 ComboBox 的默认外观替换为一个带有箭头的按钮,单击该按钮将显示一个下拉列表。你可以使用这个样式作为起点,自定义一些属性来满足你的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值