繁体   English   中英

将项目添加到 combobox 作为复选框

[英]Add items to combobox as checkbox

您好,我正在使用 XAML/WPF 创建一个 Combobox,然后使用一个 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 自动填充 combobox 我喜欢这样做通过代码。 我假设我需要做某种类型的数据绑定,但我不知道如何做,我在这里看到的答案已经有几年了,它引用了数据源,它不再是 Combobox 属性

最简单的方法是:在 XAML

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

为简单起见,创建了名为 Customer 的数据 model 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; }
}

然后,复选框将如下所示(您可以稍后对其进行数据绑定;尽管如果您想在运行时从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();

暂无
暂无

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

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