在 WPF 中,可以使用 ControlTemplate 定义一个包含 ComboBoxItem 的 ComboBox。以下是一个示例:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
            <Grid>
                <ToggleButton x:Name="PART_ToggleButton" IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                              ClickMode="Press" Focusable="False" IsHitTestVisible="False" Background="Transparent" BorderBrush="Transparent">
                    <ToggleButton.Template>
                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                            <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="3,3,23,3" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                                    <Path x:Name="Arrow" Grid.Column="1" VerticalAlignment="Center" Margin="0,0,6,0" Fill="Black" Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"/>
                                </Grid>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter TargetName="Arrow" Property="Data" Value="M0,4 L4,0 L8,4 L4,8 z"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </ToggleButton.Template>
                </ToggleButton>
                <Popup x:Name="PART_Popup" 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" BorderBrush="Black" BorderThickness="1"/>
                        <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/>
                        </ScrollViewer>
                    </Grid>
                </Popup>
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    
    <Grid>
        <ComboBox Template="{StaticResource ComboBoxTemplate}">
            <ComboBoxItem Content="Item 1"/>
            <ComboBoxItem Content="Item 2"/>
            <ComboBoxItem Content="Item 3"/>
        </ComboBox>
    </Grid>
</Window>

在上面的示例中,我们定义了一个名为'ComboBoxTemplate' 的 ControlTemplate,并将其应用于 ComboBox。ControlTemplate 的结构包括一个 ToggleButton 和一个 Popup。ToggleButton 用于显示和隐藏下拉列表,而 Popup 用于显示包含 ComboBoxItem 的下拉列表。

请注意,我们在 ComboBox 中添加了几个 ComboBoxItem,它们将显示在下拉列表中。你可以根据需要添加更多的 ComboBoxItem。

WPF 自定义 ComboBox:使用 ControlTemplate 包含 ComboBoxItem

原文地址: https://www.cveoy.top/t/topic/bHca 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录