Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将多个列添加到WPF ListView控件中_C#_Wpf - Fatal编程技术网

C# 将多个列添加到WPF ListView控件中

C# 将多个列添加到WPF ListView控件中,c#,wpf,C#,Wpf,我有这个列表视图: 这就是我尝试过的: 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(); Li

我有这个列表视图:

这就是我尝试过的:

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更新了代码,从代码后面创建了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个第一个字符串、第二个组合框和最后一个checkbox@user979033当然如果你没有得到你想要的东西,请留言,否则祝你好运。你能更新你的代码示例吗?@user979033按照你的要求更新了代码。希望这对你有帮助。
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);
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.