【问题标题】:WPF ComboBox with checkboxes带有复选框的 WPF 组合框
【发布时间】:2019-08-14 19:59:16
【问题描述】:

我正在寻找带有复选框的 WPF 组合框。我参考了以下链接并使用了 Sergey 给出的解决方案。 Looking for a WPF ComboBox with checkboxes

提供的解决方案工作正常,但我不确定如何完成与组合框的绑定。我是 WPF 的新手,所以如果我能在这方面得到任何帮助,那就太好了。 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>

这是后面的代码:

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

谢谢, 拉姆库马尔

【问题讨论】:

  • 绑定需要用到ItemSource,你用的是MVVM模式吗?
  • 如果您想使用 WPF,了解数据绑定的工作原理至关重要。在开始编写 WPF 程序之前,您或许应该先阅读 WPF 书籍以了解所有基础知识。至少你应该阅读 MSDN 上的 Data Binding Overview 文章。
  • @ArijitMukherjee 是的,我使用 ItemSource 但我将对象绑定到组合框而不是值。是的,我使用 MVVM 模式。
  • 检查我的答案@Ram_3339983

标签: c# wpf checkbox combobox


【解决方案1】:

更改您的代码:

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

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

供参考关注:

Code Project Article

【讨论】:

  • 我在更改代码时遇到“绑定约束”异常,如上所述。从代码中可以看到,FirstName 出现在 SelectableObject 类的 ObjectData 下。
  • 我在下面的链接中使用了 Sergey 建议的解决方案。 stackoverflow.com/questions/859227/…
  • 您是否尝试将 FirstName 更改为 ObjectData.FirstName?
猜你喜欢
  • 2013-06-15
  • 1970-01-01
  • 2011-09-24
  • 1970-01-01
  • 2014-10-14
  • 2018-10-19
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
相关资源
最近更新 更多