简体   繁体   English

带复选框的WPF组合框

[英]WPF ComboBox with checkboxes

I am looking for a WPF combo box with checkboxes. 我正在寻找带有复选框的WPF组合框。 I reffered to the below link and used the solution given by Sergey. 我参考了以下链接,并使用了谢尔盖(Sergey)给出的解决方案。 Looking for a WPF ComboBox with checkboxes 寻找带有复选框的WPF组合框

The solution provided works fine but I am not sure how the binding to the combo box is done. 提供的解决方案工作正常,但我不确定如何完成对组合框的绑定。 I am new to WPF so it would be great if I could get any help on this. 我是WPF的新手,所以如果能在这方面获得任何帮助,那就太好了。 XAML code: XAML代码:

<Window x:Class="WpfApplication1.StackCombo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="StackCombo" Height="338" Width="533">
<Grid>
    <ComboBox Name="cbObjects" VerticalAlignment="Center" Margin="103,140,280,138" SelectionChanged="OnCbObjectsSelectionChanged" >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding IsSelected}" Width="20" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" />
                    <TextBlock Text="{Binding}" VerticalAlignment="Center" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    <TextBlock IsHitTestVisible="False" Name="tbObjects" Text="None" VerticalAlignment="Center" Margin="113,100,268,184" />
</Grid>

This is the code Behind: 这是背后的代码:

public partial class StackCombo : Window
    {
    public StackCombo()
    {
        ObservableCollection<SelectableObject<Person>> _list;
        InitializeComponent();
        _list = new ObservableCollection<SelectableObject<Person>>();            
        _list.Add(new SelectableObject<Person>(new Person("DEF")));            
        cbObjects.ItemsSource = _list;                                   
    }

    private class Person
       {
            public Person(String firstName)
            {
                this.firstName=firstName;                    
            }
            private String firstName;          
            public String FirstName
            {
                get { return firstName; }
                set { firstName = value; }
            }               
        }

    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;
        }
    }

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

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

Thanks, Ramkumar 谢谢,Ramkumar

Change your code : 更改您的代码:

<ComboBox Name="cbObjects" VerticalAlignment="Center" Margin="103,140,280,138" SelectionChanged="OnCbObjectsSelectionChanged" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsSelected}" Width="20" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" />
                <TextBlock Text="{Binding}" VerticalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

to

<ComboBox Name="cbObjects" VerticalAlignment="Center" Margin="103,140,280,138" SelectionChanged="OnCbObjectsSelectionChanged" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Content="{Binding FirstName}" IsChecked="{Binding IsSelected}" Width="20" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" />
                <TextBlock Text="{Binding}" VerticalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

For Reference Follow : 供参考,请遵循:

Code Project Article 代码项目文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM