<Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}"> <Setter Property
时间: 2024-09-11 20:17:55 浏览: 83
在WPF (Windows Presentation Foundation) 中,`<Style>` 元素用于定义控件的外观和行为模板,如您提供的 `ToggleButtonStyle1`。它通常会针对指定的 `TargetType`(这里指 `ToggleButton` 控件),设置一组 `<Setter>`。`<Setter>` 是 `<Style>` 的一部分,用于指定当控件状态改变时属性的值应该如何变化。
例如,`Property` 属性通常引用控件的一个视觉特性,如背景色、前景色、字体样式等。`Value` 或 `Binding` 属性则定义了这个属性的新值。通过这种方式,你可以创建一种“风格”,让所有的 `ToggleButton` 在保持统一布局的基础上,可以根据需要显示不同的视觉效果。
举个例子,可能的设置如下:
```xml
Setter Property="Background"
Value="{DynamicResource MyToggleBackground}"
TargetProperty="(Control.Background)"
```
这将设定按钮的背景颜色从资源 `MyToggleBackground` 获取,并绑定到 `Control.Background` 属性上,所有继承自 `ToggleButton` 的控件都将应用这个样式。
相关问题
WPF ComboBox style
以下是一个简单的 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 的默认外观替换为一个带有箭头的按钮,单击该按钮将显示一个下拉列表。你可以使用这个样式作为起点,自定义一些属性来满足你的需求。
WPF ComboBox 美化
WPF ComboBox 是一个很常用的控件,但是默认的样式有些简单,不够美观。下面提供一种简单的方式实现 WPF ComboBox 的美化:
1. 在 XAML 中定义一个 Style,用于修改 ComboBox 的样式:
```
<Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">
<Setter Property="Background" Value="#FFFFFF" />
<Setter Property="Foreground" Value="#000000" />
<Setter Property="BorderBrush" Value="#D3D3D3" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Padding" Value="6,2,25,2" />
<Setter Property="Margin" Value="0,5,0,0" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid>
<ToggleButton x:Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}" Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
</ToggleButton>
<ContentPresenter x:Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" />
<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="#FFFFFF" BorderThickness="1" BorderBrush="#D3D3D3" />
<ScrollViewer SnapsToDevicePixels="True">
<ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
2. 定义一个样式模板,用于修改 ComboBox 中的 ToggleButton 样式:
```
<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
<Setter Property="Focusable" Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Border x:Name="Border" Grid.ColumnSpan="2" Background="#FFFFFF" BorderBrush="#D3D3D3" BorderThickness="1" CornerRadius="3" />
<Border Grid.Column="0" Background="#FFFFFF" Margin="1" BorderBrush="#D3D3D3" BorderThickness="0,0,1,0">
<ContentPresenter HorizontalAlignment="Left" Margin="6,0,0,0" VerticalAlignment="Center" />
</Border>
<Path x:Name="Arrow" Grid.Column="1" Fill="#000000" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Fill" TargetName="Arrow" Value="#FFFFFF" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Border" Value="#F2F2F2" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#D3D3D3" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
3. 在 ComboBox 控件中引用上述定义的 Style:
```
<ComboBox Style="{StaticResource ComboBoxStyle}" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" />
```
通过上述方式,可以实现 WPF ComboBox 的美化。当然,还可以根据实际需求进一步修改样式。
阅读全文