WPF中,Combobox下拉框有CheckBox(单选,或多选)

一、背景

WPF中,Combobox下拉框需要带有CheckBox勾选框,且需要根据项目不同要求,支持单选或多选功能。

二、单选功能

<ComboBox x:Name="comboBox" 
          IsEditable="True" 
          IsReadOnly="True" 
          IsDropDownOpen="False" 
          Width="200" Height="30" 
          HorizontalAlignment="Left" VerticalContentAlignment="Center" 
          Margin="10">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding}"
                      IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
<TextBox x:Name="textBox" 
         Text="{Binding ElementName=comboBox,Path=Text}" 
         HorizontalAlignment="Left" VerticalContentAlignment="Center" 
         Margin="10" 
         Width="200" Height="30" 
         IsReadOnly="True" />
 public Combobox_Listbox()
 {
     InitializeComponent();

     selectData();
 }

 public void selectData()
 {
     // 模拟获取数据源
     SQLHelper db = new SQLHelper();
     string sql = string.Format(@"select list from Listbox where content_one='纠纷投诉' and content_two='患者类别' ORDER BY ID");
     DataSet num = db.GetDataSet(sql);

     // 创建一个 DataTable 作为数据源
     DataTable dt = num.Tables[0];

     // 将 DataTable 中的数据转换成一个字符串数组
     string[] items = dt.AsEnumerable().Select(row => row.Field<string>("list")).ToArray();

     // 将字符串数组绑定到 ComboBox 的 ItemsSource 属性上
     comboBox.ItemsSource = items;
 }

三、单选功能

<ComboBox x:Name="comboBox_p" Width="200" Height="30" HorizontalAlignment="Left" Margin="10">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding list}" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ComboBoxItem}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
public void selectData2()
{
    SQLHelper db = new SQLHelper();
    string sql = string.Format(@"select list from Listbox where content_one='纠纷投诉' and content_two='患者类别' ORDER BY ID");
    DataSet result = db.GetDataSet(sql);

    // 将查询结果作为 ComboBox 的数据源
    comboBox_p.ItemsSource = result.Tables[0].DefaultView;

}

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
你可以使用ComboBox的样式来更改下拉框的背景颜色。以下是一个示例,将ComboBox下拉框背景颜色设置为灰色: ```xml <ComboBox> <ComboBox.Resources> <Style TargetType="{x:Type ComboBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBox}"> <Grid> <ToggleButton Name="ToggleButton" ClickMode="Press" Focusable="False" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="2" Grid.Row="0" Style="{StaticResource ComboBoxToggleButton}" Background="Gray"/> <ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" /> <TextBox x:Name="PART_EditableTextBox" Style="{x:Null}" Template="{StaticResource ComboBoxTextBox}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="3,3,23,3" Focusable="True" Background="Transparent" Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}"/> <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide"> <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}"> <Border x:Name="DropDownBorder" Background="White" BorderThickness="1" BorderBrush="Black"/> <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> <StackPanel IsItemsHost="True" /> </ScrollViewer> </Grid> </Popup> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="PART_EditableTextBox" Property="Foreground" Value="DimGray"/> </Trigger> <Trigger Property="IsGrouping" Value="True"> <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> </Trigger> <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True"> <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/> <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/> </Trigger> <Trigger Property="IsEditable" Value="True"> <Setter Property="IsTabStop" Value="False"/> <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/> <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ComboBox.Resources> <ComboBoxItem>Item 1</ComboBoxItem> <ComboBoxItem>Item 2</ComboBoxItem> <ComboBoxItem>Item 3</ComboBoxItem> </ComboBox> ``` 在这个示例,我们将ComboBox的模板更改为一个包含ToggleButton、ContentPresenter、TextBox和Popup的Grid。ToggleButton用于展开和收起下拉框,ContentPresenter用于显示当前选择的项,TextBox用于允许用户编辑文本,Popup用于显示下拉框的项。 我们可以使用Background属性将ToggleButton的背景颜色设置为灰色。在这个示例,我们还更改了其他控件的样式和模板,以便它们与ToggleButton的颜色相匹配。 请注意,这只是一个示例,你可以根据你的需要自定义ComboBox的外观和行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亮猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值