Search code examples
c#wpfxamlcheckboxcombobox

Add items to combobox as checkbox


Hello I am using XAML/WPF to create a Combobox and then using an XML to populate it

Using the suggestion below this is my updated code and now it works!!

This is my XAML using the suggestions given below

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

This is my 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>

And this is the code that is used to populate the 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;

All this works but I would like to have the added items inside the Combobox be in the form of a checklist I done it through the XAML by adding Checkbox items manually but since I am populating the combobox automatically by reading the XML I like to do it through code. I assume I need to do some type of Data Binding but I do not know how and the answers I seen here are a few years old which reference DataSource which its not a Combobox attribute anymore


Solution

  • Simpliest way would be : in XAML

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

    For simplicity created data model class called Customer

    public class Customer
    {
      public string Name {get;set;}
    }
    

    Then your method would be like

    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;