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

时间:2020-08-04 11:09:09

标签: c# wpf xaml checkbox combobox

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

使用下面的建议,这是我更新的代码,现在可以正常使用!!

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

<?php
function product_discount($price)
{
  if($price<5000)
  {
    $dis=$price*5/100;
  }
  else
  {
    $dis=$price*10/100;
  }

  $total=$price-$dis;

  echo "ส่วนลดที่ได้::". $dis."บาท"."</br>";
  echo "ราคาสุทธิ::". $total."บาท"."</br>";
}

$price1=product_discount(1000);
$price2=product_discount(5000);
?>

这是我的XML

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

这是用于填充customer_comboBox的代码

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

所有这些都可以,但是我希望组合框内添加的项目采用清单的形式 我通过手动添加Checkbox项通过XAML进行了此操作,但是由于我通过读取XML自动填充组合框,因此我希望通过代码来完成此操作。我以为我需要做某种类型的数据绑定,但是我不知道怎么做,我在这里看到的答案已经有几年历史了,它引用了不再是Combobox属性的DataSource

2 个答案:

答案 0 :(得分:1)

最简单的方法是: 在XAML中

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

为简单起见,创建了名为Customer的数据模型类

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;

答案 1 :(得分:0)

首先,创建一个业务类:

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添加/删除项目,我的建议是使用{{1}以外的其他内容}作为数据绑定的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>

稍后,要获取所选项目:

        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;
相关问题