【问题标题】:Add several columns into WPF ListView control [duplicate]将几列添加到 WPF ListView 控件中[重复]
【发布时间】:2016-12-20 12:49:45
【问题描述】:

我有这个ListView

<ListView name="listView1" />

我想在代码后面添加 Row 和几个字段:

1. string
2. Combobox
3. Checkbox

这是我尝试过的:

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

目前,结果是在我看到的每一列中Object[] Array

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    更新了代码以从后面的代码 (C#) 创建 WPF ListView 列和数据显示。希望这可以帮助你:

    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.
    

    【讨论】:

    • @user979033 如果您有问题,请先写评论,然后再投反对票并标记删除,这将有助于了解哪里出了问题。
    • 我的意思是每行应该包含这3个控制器,目前每行只包含1个(第一个字符串,第二个组合框和最后一个复选框)
    • @user979033 当然,如果你没有得到你想要的东西,请留言,否则祝你好运。
    • 你能更新你的代码示例吗?
    • @user979033 根据您的要求更新了代码。希望对您有所帮助。
    猜你喜欢
    • 2013-03-29
    • 2010-10-14
    • 1970-01-01
    • 2012-04-14
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多