ComboBox

本文介绍如何在 WPF 中定制 ComboBox 的样式与模板,包括定义 ComboBox 的外观、下拉效果及项模板等,通过具体 XAML 代码示例展示实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

 <DataTemplate x:Key="ComboBoxItemTemplate" >
        <Grid Height="45">
            <TextBlock Text="{Binding }" VerticalAlignment="Center"/>
        </Grid>
    </DataTemplate>



    <ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
        <Grid x:Name="MainGrid" SnapsToDevicePixels="True">
            <Popup x:Name="PART_Popup" AllowsTransparency="True" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                <Border x:Name="DropDownBorder" BorderBrush="#72919DB0" BorderThickness="1 0 1 1" CornerRadius="3" Background="{TemplateBinding Background}">
                    <Border.Effect>
                        <DropShadowEffect ShadowDepth="0" Color="#FF919DB0" Opacity="0.45"/>
                    </Border.Effect>
                    <ScrollViewer x:Name="DropDownScrollViewer">
                        <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
            </Popup>
            <Border  x:Name="Chrome" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" 
                                            Background="{TemplateBinding Background}" CornerRadius="5" SnapsToDevicePixels="True">
                <Grid>
                    <Grid HorizontalAlignment="Right" Width="50" Background="#f5f9fb">
                        <Path x:Name="Arrow" Data="M0,0L7,9 14,0z" Fill="#97afbf" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                    <Toolkit:ImageCheckBox IsChecked="{Binding IsDropDownOpen,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
                                           Background="Transparent" CheckedBackground="Transparent"/>
                </Grid>

            </Border>
            <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Grid>
    </ControlTemplate>

    <Style x:Key="ComboBoxStyle" TargetType="ComboBox">
        <Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"/>
        <Setter Property="ItemTemplate" Value="{StaticResource ComboBoxItemTemplate}"/>
        <Setter Property="BorderBrush" Value="#dddddd"/>
        <Setter Property="Background" Value="Pink"/>
        <Setter Property="BorderThickness" Value="1"/>
    </Style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值