将项目作为复选框添加到组合框

Add items to combobox as checkbox

您好,我正在使用 XAML/WPF 创建组合框,然后使用 XML 填充它

使用下面的建议这是我更新的代码,现在它可以工作了!!

这是我的 XAML 使用下面给出的建议

    <ComboBox x:Name="customer_comboBox" HorizontalAlignment="Left" Margin="83,259,0,0" VerticalAlignment="Top" Width="172" SelectionChanged="customer_comboBox_SelectionChanged" >
         <ComboBox.ItemTemplate>
             <DataTemplate>
                    <CheckBox Content="{Binding}"/>
            </DataTemplate>
         </ComboBox.ItemTemplate>
    </ComboBox>

这是我的XML

<?xml version="1.0" encoding="utf-8" ?>
<ComboBox>
  <Customer name="John">
    <Data>
      <System>Linux</System>
    </Data>
  </Customer>
  <Customer name="Fernando">
    <Data>
      <System>Microsoft</System>
      <System>Mac</System>
    </Data>
  </Customer>
</ComboBox>

这是用于填充 customer_comboBox

的代码
 XmlDocument doc = new XmlDocument();
 doc.Load(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +@"\comboBox.xml");
 XmlNodeList customerList = doc.SelectNodes("ComboBox/Customer");
 List<string> customers = new List<string>();

 foreach (XmlNode node in customerList)
 {
     customers.Add(child.InnerText);
 }
 customer_comboBox.ItemsSource = customers;

所有这些都有效,但我希望 Combobox 中添加的项目以清单的形式出现 我通过 XAML 手动添加复选框项来完成此操作,但由于我通过读取 XML 自动填充组合框,所以我喜欢通过代码来完成。我想我需要做某种类型的数据绑定,但我不知道如何做,我在这里看到的答案已经有几年了,它引用了 DataSource,它不再是 Combobox 属性

最简单的方法是: 在 XAML

<ComboBox x:Name="customer_comboBox" ...all other themings...>
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <CheckBox Content="{Binding Name}"/>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

为简单起见,创建了名为 Customer

的数据模型 class
public class Customer
{
  public string Name {get;set;}
}

那么你的方法就是

XmlDocument doc = new XmlDocument();
 doc.Load(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +@"\comboBox.xml");
 XmlNodeList customerList = doc.SelectNodes("ComboBox/Customer");

List<Customer> customers = new List<Customer>()

 foreach (XmlNode node in customerList)
 {
   customers.Add(new Customer(){ Name = node.Attributes["name"].InnerText });
 }

customer_comboBox.ItemsSource = customers;

首先,创建一个企业class:

public class Customer: INotifyPropertyChanged
{
    // INotifyPropertyChanged implementation
    public virtual event PropertyChangedEventHandler PropertyChanged;
    public virtual void NotifyPropertyChanged(string propName)
        => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
    
    public string Name { get; set; }

    // Property to build to IsChecked of CheckBox
    public bool IsSelected { get; set; }
}

然后,复选框将如下所示(您可以稍后对其进行数据绑定;尽管如果您想在运行时动态 add/delete 来自 ComboBox 的项目,我的建议是使用除IList 作为数据绑定 ItemsSource,例如 ObservableCollection)

的某些实现
    <ComboBox Name="cbx">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding IsSelected}"
                       Width="20" />
                    <TextBlock Text="{Binding Name}"
                       Width="100" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

以下是用数据填充 ComboBox 的方法:

        List<Customer> customers = new List<Customer>();
        // populate list instead of ComboBox from xml data source here...

        // then set the list as source
        cbx.ItemsSource = customers;

稍后,获取所选项目:

 List<Customer> selectedCustomers = ((List<Customer>)cbx.ItemsSource)
     .Where(a => a.IsSelected).ToList();