0

I have this ListView:

<ListView name="listView1" />

And I want to add Row with several fields in code behind:

1. string
2. Combobox
3. Checkbox

This is what I have tried:

List<string> list = new List<string>();
tests.Add("Select country");
tests.Add("USA");
tests.Add("Germany");

ComboBox combobox = new ComboBox();
combobox.ItemsSource = options;

CheckBox checkbox = new CheckBox();

ListViewItem itm;
object[] abjects = new object[3];
abjects[0] = "my name" as string;
abjects[1] = combobox as ComboBox;
abjects[2] = checkbox as CheckBox;

itm = new ListViewItem();
itm.Content = abjects;
listView1.Items.Add(itm);

Currently, the result is that inside every column I see Object[] Array

jschroedl
  • 4,916
  • 3
  • 31
  • 46
user979033
  • 5,430
  • 7
  • 32
  • 50

1 Answers1

-1

Updated code to create WPF ListView columns and data display from code behind(C#). Hope this may help you:

List<string> countryList = new List<string>();
countryList.Add("Select country");
countryList.Add("USA");
countryList.Add("Germany");


var layoutGridView = new GridView(); //Create layout for ListView.

// Create Display Template and Bindings
FrameworkElementFactory nameFactory = new FrameworkElementFactory(typeof(TextBlock));
nameFactory.Name = "tbkName";
nameFactory.SetBinding(TextBlock.TextProperty, new Binding("Name")); // Assign Property Binding paths for the collection bound to ListView.

FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox));
comboFactory.Name = "cmbCountry";
comboFactory.SetValue(ComboBox.ItemsSourceProperty, countryList); // Assign default list to display in dropdown.
comboFactory.SetBinding(ComboBox.SelectedValueProperty, new Binding("Country")); // Assign value to select from dropdown.

FrameworkElementFactory checkBoxFactory = new FrameworkElementFactory(typeof(CheckBox));
checkBoxFactory.Name = "chkSelected";
checkBoxFactory.SetValue(CheckBox.IsCheckedProperty, new Binding("IsSelected"));

//Define columns with the corresponding cell templates
layoutGridView.Columns.Add(new GridViewColumn
{
    Header = "Name",
    CellTemplate = new DataTemplate
    {
        VisualTree = nameFactory    //First(text) column display template
    }
});

layoutGridView.Columns.Add(new GridViewColumn
{
    Header = "State",
    CellTemplate = new DataTemplate
    {
        VisualTree = comboFactory   //Second(Combobox) column display template
    }
});

layoutGridView.Columns.Add(new GridViewColumn
{
    Header = "Is Selected",
    CellTemplate = new DataTemplate
    {
        VisualTree = checkBoxFactory    //Third(Checkbox) column display template
    }
});

listView1.View = layoutGridView;    // Assign the display template to ListView.

//Data Binding to listview.
List<MyData> data = new List<MyData>
{
    new MyData { Name="Abc", Country= "USA", IsSelected=true},
    new MyData { Name="Def", Country= "Germany", IsSelected=false}
};

listView1.ItemsSource = data;   //Assign data to ListView's ItemsSource property.
Siva Gopal
  • 3,474
  • 1
  • 25
  • 22