【WPF学习手记】WPF带有ToggleButton的ComboBox的样式设计

目的:ComboBox显示选中项的图标。

    选中某项,ComboBox显示对应图标,并且ComboBox设置为选中和未选中两种状态。

  • xaml代码
<Window x:Class="ComboBoxStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ComboBoxStyle"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid Margin="5" Width="88" Height="68">
            <ComboBox Name="comboBox" ItemsSource="{Binding ComboBoxItems}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding Image}" Margin="0,0,5,0" Height="30" Width="30"/>
                            <Label Content="{Binding Name}" HorizontalContentAlignment="Left" VerticalContentAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <ToggleButton Name="tgButton" BorderBrush="Transparent" Margin="0,0,20,0">
                <ToggleButton.Content>
                    <StackPanel>
                        <Image Height="32" Width="32" DataContext="{Binding ElementName=comboBox, Path=SelectedItem}" Source="{Binding Image}"/>
                        <Label HorizontalContentAlignment="Center" VerticalContentAlignment="Bottom"
                                   DataContext="{Binding ElementName=comboBox, Path=SelectedItem}" Content="{Binding Name}"/>
                    </StackPanel>
                </ToggleButton.Content>
            </ToggleButton>
        </Grid>
    </Grid>
</Window>
  • 后台代码
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace ComboBoxStyle
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<CDrawingFlag> ComboBoxItems { set; get; }
        public MainWindow()
        {
            InitializeComponent();
            LoadOperations();
        }

        private void LoadOperations()
        {
            List<string> imagePath = new List<string>
            {
                @"images\Ellipse_blue.png",
                @"images\Line_blue.png",
                @"images\Rectangle_blue.png",
                @"images\Text_blue.png",
                @"images\Angle_blue.png",
                @"images\Edit_blue.png",
            };

            List<string> flag = new List<string>
            {
                "Ellipse",
                "Line",
                "Rectangle",
                "Text",
                "Angle",
                "Edit"
            };

            ComboBoxItems = new ObservableCollection<CDrawingFlag>();
            for (int i = 0; i < imagePath.Count; i++)
            {
                ComboBoxItems.Add(new CDrawingFlag() { Image = imagePath[i], Name = flag[i] });
            }
            comboBox.ItemsSource = ComboBoxItems;
            comboBox.SelectedIndex = 0;
        }
    }

    public class CDrawingFlag
    {
        public string Image { set; get; }
        public string Name { set; get; }
    }
}

 

自定义 WPF ComboBox样式,步骤如下: 1. 创建一个新的 WPF 目。 2. 在目中打开 MainWindow.xaml 文件。 3. 在窗口中添加一个 ComboBox 控件。 4. 在 ComboBox 控件中添加一些。 ```xml <ComboBox> <ComboBoxItem>Item 1</ComboBoxItem> <ComboBoxItem>Item 2</ComboBoxItem> <ComboBoxItem>Item 3</ComboBoxItem> </ComboBox> ``` 5. 现在我们将使用模板来自定义 ComboBox样式。右键单击 ComboBox 控件,选择“编辑模板”->“编辑复制的模板”->“创建”。 6. 在“对象和时间线”窗口中,找到名为“ComboBox”的控件模板。 7. 在模板中找到“Border”元素,并添加以下属性: ```xml <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3"> ``` 8. 在“ToggleButton”元素中添加以下属性: ```xml <ToggleButton x:Name="toggleButton" Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource ComboBoxToggleButton}"/> ``` 这将在 ComboBox 上添加一个箭头按钮。 9. 在“Popup”元素中添加以下属性,以更改下拉列表的背景颜色: ```xml <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="Black" CornerRadius="3"/> <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/> </ScrollViewer> </Grid> </Popup> ``` 10. 最后,添加以下样式目中: ```xml <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}"> <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" CornerRadius="3" BorderThickness="1,1,1,1" Background="White" BorderBrush="Black"/> <Border Grid.Column="0" CornerRadius="3" Margin="1" Background="{TemplateBinding Background}" BorderThickness="0,0,0,0"/> <Path x:Name="Arrow" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" Fill="Black"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="ToggleButton.IsChecked" Value="True"> <Setter TargetName="Border" Property="Background" Value="#FFC1E0FF"/> <Setter TargetName="Border" Property="BorderBrush" Value="#FFA5CBE2"/> <Setter TargetName="Arrow" Property="Fill" Value="#FF000000"/> </Trigger> <Trigger Property="UIElement.IsEnabled" Value="False"> <Setter TargetName="Border" Property="Background" Value="#FFF4F4F4"/> <Setter TargetName="Border" Property="BorderBrush" Value="#FFADB2B5"/> <Setter TargetName="Arrow" Property="Fill" Value="#FF808080"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 11.运行程序,现在你将看到一个美丽的自定义 ComboBox 样式。 希望这能帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值