Looking for a WPF ComboBox with checkboxes

一个人想着一个人 提交于 2019-11-26 07:34:45

问题


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).

alt text http://www.angryhacker.com/toys/up.png


回答1:


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>



回答2:


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!




回答3:


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).



来源:https://stackoverflow.com/questions/859227/looking-for-a-wpf-combobox-with-checkboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!