寻找带有复选框的 WPF ComboBox

发布于 2024-07-19 14:03:14 字数 68 浏览 8 评论 0原文

我的谷歌技能让我失败了。 任何人都听说过 WPF 这样的控件。 我试图让它看起来像这样(winforms 屏幕截图)。

My google skills fail me. Anyone heard of a control like that for WPF. I am trying to make it look like this (winforms screenshot).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不会写诗 2024-07-26 14:03:14

您可以通过设置组合框的 DataTemplate 自行完成此操作。 本文向您展示了如何操作 - 对于列表框,但原则是相同的。


另一篇文章 here 也许更适合您想要做的事情,只需将项目模板的第一列设置为复选框并将其绑定到业务对象上的布尔值即可。

<ComboBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <CheckBox IsChecked="{Binding IsSelected}"
                       Width="20" />
            <TextBlock Text="{Binding DayOfWeek}"
                       Width="100" />
        </StackPanel>
    </DataTemplate>
</ComboBox.ItemTemplate>

You can do this yourself by setting the DataTemplate of the combo box. This article shows you how - for a listbox, but the principle is the same.


Another article here is perhaps a better fit for what you are trying to do, simple set the first column of the item template to be a checkbox and bind it to a bool on your business object.

<ComboBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <CheckBox IsChecked="{Binding IsSelected}"
                       Width="20" />
            <TextBlock Text="{Binding DayOfWeek}"
                       Width="100" />
        </StackPanel>
    </DataTemplate>
</ComboBox.ItemTemplate>
蓝眼泪 2024-07-26 14:03:14

这是我的组合框。 我使用 Martin Harris 代码和此链接中的代码 当选择为空时,WPF ComboBox 可以显示替代文本吗?

<ComboBox Name="cbObjects" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="2,2,6,0" SelectionChanged="OnCbObjectsSelectionChanged" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsSelected}" Width="20" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" />
                <TextBlock Text="{Binding ObjectData}" VerticalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
<TextBlock IsHitTestVisible="False" Name="tbObjects" Text="Выберите объекты..." Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="6,2,6,0" />

数据源小类:

public class SelectableObject <T> {
    public bool IsSelected { get; set; }
    public T ObjectData { get; set; }

    public SelectableObject(T objectData) {
        ObjectData = objectData;
    }

    public SelectableObject(T objectData, bool isSelected) {
        IsSelected = isSelected;
        ObjectData = objectData;
    }
}

有两个处理程序 - 一个用于处理单击的 CheckBox,一个用于为 ComboBox 形成文本。

private void OnCbObjectCheckBoxChecked(object sender, RoutedEventArgs e) {
    StringBuilder sb = new StringBuilder();
    foreach (SelectableObject<tblObject> cbObject in cbObjects.Items) 
    {
        if (cbObject.IsSelected)
            sb.AppendFormat("{0}, ", cbObject.ObjectData.Description);
    }
    tbObjects.Text = sb.ToString().Trim().TrimEnd(',');
}

private void OnCbObjectsSelectionChanged(object sender, SelectionChangedEventArgs e) {
    ComboBox comboBox = (ComboBox)sender;
    comboBox.SelectedItem = null;
}

对于 ComboBox.ItemsSource,我使用

ObservableCollection<SelectableObject<tblObject>> 

其中 tblObject 是我的对象的类型,我想在 ComboBox 中显示其列表。

我希望这段代码对某人有用!

There is my combobox. I use Martin Harris code and code from this link Can a WPF ComboBox display alternative text when its selection is null?

<ComboBox Name="cbObjects" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="2,2,6,0" SelectionChanged="OnCbObjectsSelectionChanged" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsSelected}" Width="20" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" />
                <TextBlock Text="{Binding ObjectData}" VerticalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
<TextBlock IsHitTestVisible="False" Name="tbObjects" Text="Выберите объекты..." Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="6,2,6,0" />

Small class for datasource:

public class SelectableObject <T> {
    public bool IsSelected { get; set; }
    public T ObjectData { get; set; }

    public SelectableObject(T objectData) {
        ObjectData = objectData;
    }

    public SelectableObject(T objectData, bool isSelected) {
        IsSelected = isSelected;
        ObjectData = objectData;
    }
}

And there is two handler - one for handling CheckBox clicked and one for forming Text for ComboBox.

private void OnCbObjectCheckBoxChecked(object sender, RoutedEventArgs e) {
    StringBuilder sb = new StringBuilder();
    foreach (SelectableObject<tblObject> cbObject in cbObjects.Items) 
    {
        if (cbObject.IsSelected)
            sb.AppendFormat("{0}, ", cbObject.ObjectData.Description);
    }
    tbObjects.Text = sb.ToString().Trim().TrimEnd(',');
}

private void OnCbObjectsSelectionChanged(object sender, SelectionChangedEventArgs e) {
    ComboBox comboBox = (ComboBox)sender;
    comboBox.SelectedItem = null;
}

For ComboBox.ItemsSource I use

ObservableCollection<SelectableObject<tblObject>> 

where tblObject is type of my object, a list of which I want to display in ComboBox.

I hope this code is useful to someone!

晒暮凉 2024-07-26 14:03:14

尝试一下 CheckComboBox 来自 扩展 WPF 工具包
对我来说,主要优点是有两个用于绑定的列表:

  • 所有可供选择的项目
  • 只是选定的项目

我发现这种方法更实用。 此外,您还可以指定要绑定的集合的 valuedisplay 成员。

如果您不想使用 CheckComboBox 带来一堆其他控件,您可以获取 源代码,非常简单(需要带上源代码) com/xceedsoftware/wpftoolkit/blob/master/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Primitives/Selector.cs" rel="nofollow noreferrer">选择器类)。

Give a try to CheckComboBox from Extended WPF Toolkit.
The main advantage for me is having two lists for binding:

  • all items available for selection
  • just selected items

I find this approach more practical. In addition you can specify value and display members of the collections you're binding.

If you don't want to bring a bunch of other controls with CheckComboBox, you can get the source code of it, it's pretty straightforward (need to bring Selector class as well).

橙幽之幻 2024-07-26 14:03:14

带有复选框的组合框

<ComboBox Height="16" Width="15">
    <CheckBox Content="First Checkbox" />
    <CheckBox Content="Second Checkbox" />
    <CheckBox Content="Third Checkbox" />
    <TextBlock Text="Some Text" />
</ComboBox>

令人惊讶的是,所提供的答案对我不起作用,我尝试了许多变体,并不断收到有关复选框不属于组合框的错误消息,并且数据上下文似乎已损坏。

最后,我不需要做任何涉及数据模板或任何背后代码的事情,并且我的绑定工作正常(示例中未显示)

ComboBox with Checkboxes

我必须说,在阅读了所有答案后,我很高兴发现这是多么容易。

ComboBox with Checkboxes

<ComboBox Height="16" Width="15">
    <CheckBox Content="First Checkbox" />
    <CheckBox Content="Second Checkbox" />
    <CheckBox Content="Third Checkbox" />
    <TextBlock Text="Some Text" />
</ComboBox>

The provided answers surprisingly didn't work for me, I tried many variations and kept getting error messages about the checkbox not being part of combobox and the data context seemed to be broken.

In the end I didn't have to do anything involving data templates or any code behind and my bindings are working fine (not shown in example)

ComboBox with Checkboxes

I must say I'm happy with how easy this turned out to be after reading all the answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文