创建WPF自定义组合框

本文关键字:组合 自定义 WPF 创建 | 更新日期: 2023-09-27 18:11:23

我想做的是一个有最喜欢的值在顶部的组合框,具有不同的背景颜色和按钮。现在我有:

<UserControl x:Class="ComboBoxWithButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"
             Name="root"
             d:DesignWidth="300" Height="25">
    <ComboBox 
         x:Name="ComboBoxBtn" 
         VerticalAlignment="Top" 
         HorizontalAlignment="Left" 
         Margin="0,0,0,-1" 
         Width="300" 
         ItemsSource="{Binding Source, RelativeSource={RelativeSource AncestorType=UserControl}}"
         SelectedItem="{Binding Path=Selected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}">
       <ComboBox.Resources>
            <Style TargetType="ComboBoxItem">
                ????
            </Style>
        </ComboBox.Resources>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.Style>
                        <Style TargetType="Grid">
                            <Setter Property="Background" Value="#FFE6E6FA"/>
                        </Style>
                    </Grid.Style>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Label Content="{Binding}" Width="250" />
                    <Button Grid.Column="1" Command="{Binding CommandButton, ElementName=root}"
                            CommandParameter="{Binding}">+</Button>
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</UserControl>

现在我有一个添加按钮,所以我可以添加我的项目作为收藏夹。但是我现在想要的是,基于我将它表示为最喜欢或不喜欢的项目。

Case是一个收藏夹,有一个不同的背景颜色和一个[-]按钮(删除)。大小写不一样,背景是白色的,并有一个[+]。

创建WPF自定义组合框

看看ContentControl在你使用Button的地方是否有帮助。

 <ComboBox.ItemTemplate>
 <DataTemplate>
 <ContentControl>
    <Style TargetType="ContentControl">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ...}" Value="True">
                <Setter Property="Content">
                    <Setter.Value>
                            <Grid>
                                <Grid.Style>
                                    <Style TargetType="Grid">
                                        <Setter Property="Background" Value="#FFE6E6FA"/>
                                    </Style>
                                </Grid.Style>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Label Content="{Binding}" Width="250" />
                                <Button Grid.Column="1" Command="{Binding CommandButton, ElementName=root}"
                        CommandParameter="{Binding}">+</Button>
                            </Grid>
                        </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding ...}" Value="False">
                <Setter Property="Content">
                    <Setter.Value>
                            <Grid>
                                <Grid.Style>
                                    <Style TargetType="Grid">
                                        <Setter Property="Background" Value="Yellow"/>
                                    </Style>
                                </Grid.Style>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Label Content="{Binding}" Width="250" />
                                <Button Grid.Column="1" Command="{Binding CommandButton, ElementName=root}"
                                    CommandParameter="{Binding}">-</Button>
                            </Grid>
                        </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ContentControl>
</DataTemplate>
</ComboBox.ItemTemplate>