C#动态地在网格视图中添加控件

时间:2016-05-20 09:43:45

标签: c# wpf listview

我有一个列表视图,其中包含使用网格视图填充的多个单元格。我想动态地为每一行添加一个文本框和按钮。我该怎么做?

我的尝试:

 public void PopulateCalibrationParam(IList<A2LCharacteristic> CalibrationParam)
    {
        int rowcount = 0;
        CalibrationGrid.RowDefinitions.Clear();
        CalibrationGrid.ColumnDefinitions.Clear();

        RowDefinition rd = new RowDefinition();
        rd.Height = new GridLength();
        CalibrationGrid.RowDefinitions.Add(rd);
        CalibrationGrid.RowDefinitions.Add(new RowDefinition());
        CalibrationGrid.ColumnDefinitions.Add(new ColumnDefinition());
        Label t1 = new Label();
        t1.Content = "Calibration Variables";
        Grid.SetColumn(t1, 0);
        Grid.SetRow(t1, rowcount);
        CalibrationGrid.Children.Add(t1);


        ListView l1 = new ListView();

        GridView g1 = new GridView();

        g1.AllowsColumnReorder = true;


        GridViewColumn g2 = new GridViewColumn();
        g2.Header = "Name";
        g2.Width = 200;
        g2.DisplayMemberBinding = new Binding("Name");
        g1.Columns.Add(g2);

        GridViewColumn g5 = new GridViewColumn();
        g5.Header = "DataType";
        g5.Width = 200;
        g5.DisplayMemberBinding = new Binding("DataType");
        g1.Columns.Add(g5);

        GridViewColumn g3 = new GridViewColumn();
        g3.Header = "Value";
        g3.Width = 200;
        g3.DisplayMemberBinding = new Binding("Value");


        g1.Columns.Add(g3);

        GridViewColumn g4 = new GridViewColumn();
        g4.Header = "Action";
        g4.Width = 200;
        g4.DisplayMemberBinding = new Binding("ToDo");
        g1.Columns.Add(g4);
        l1.View = g1;


        Grid.SetRow(l1, rowcount + 1);
        CalibrationGrid.Children.Add(l1);


        for (int i = 0; i < CalibrationParam.Count; i++)
        {
            Label lb1 = new Label();
            lb1.Content = CalibrationParam[i].Name;

            Label lb2 = new Label();
            lb2.Name = CalibrationParam[i].RecordLayoutRef.Parameters[0].DataType.ToString();

            DataTemplate dd = new DataTemplate();
            TextBox tb = new TextBox();



            Button b1 = new Button();
            b1.Content = "OK";
            b1.Height = 30;
            b1.Width = 150;
            l1.Items.Add(new User() { Name = lb1.Content, DataType = lb2.Name, Value = tb, ToDo = b1 });



        }



    }

   public class User
   {
       public object Name { get; set; }

       public string DataType { get; set; }

       public Control Value
       {
           get;

           set;

       }

       public Control ToDo { get; set; }
   }

我得到了:

enter image description here

我想要第3列和第4列中的文本框和按钮。

请帮忙。我找到了类似的problem,但无法解决。请帮忙。

1 个答案:

答案 0 :(得分:1)

您需要为CellTemplate创建GridView
XAML (为了我的昵称)

<GridViewColumn>
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBox Text="{Binding}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
<GridViewColumnHeader Content="Action"/>  

C#

public partial class MainWindow : Window
{
   public MainWindow()
   {
      InitializeComponent();
      mylistview.ItemsSource = CreateTable().DefaultView;
    }

public DataTable CreateTable()//create a datatable
{
   DataTable dt = new DataTable();

   dt.Columns.Add("Name", typeof(string));
   dt.Columns.Add("Number", typeof(double));



   dt.Rows.Add("A", 1234.78);
   dt.Rows.Add("B", 12.0);
   dt.Rows.Add("C", 7.89);

   return dt;

}


private void Button_Click_1(object sender, RoutedEventArgs e)
{
    foreach (DataColumn DCol in CreateTable().Columns)
     {
        GridViewColumn gvc3 = new GridViewColumn();
        Binding bind = new Binding(DCol.ColumnName);

        if (DCol.DataType == typeof(System.Double)) //add the double value typed columns
         {
             bind.StringFormat = "{0:0.000}";

             DataTemplate cell = new DataTemplate(); // create a datatemplate
             FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBlock));
             factory.SetBinding(TextBlock.TextProperty, bind);//the second parameter should be bind
             factory.SetValue(TextBlock.WidthProperty, 50D);
             factory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);//set the text align to right
             factory.SetValue(TextBlock.BackgroundProperty, System.Windows.Media.Brushes.Blue);
             factory.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.Yellow));

             cell.VisualTree = factory;

             gvc3.CellTemplate = cell;
             gvc3.Header = DCol.ColumnName;
             myGridView.Columns.Add(gvc3);
       }
       else       //add other columns
       {
            gvc3.DisplayMemberBinding = bind;
            gvc3.Header = DCol.ColumnName;
            myGridView.Columns.Add(gvc3);
        }

    }    
 }

}//MainWindow       

将此作为您的Visual Tree:

<StackPanel Name="myStackPanel">
    <ListView Name="mylistview">
        <ListView.View>
            <GridView x:Name="myGridView">    
            </GridView>
        </ListView.View>
    </ListView>
<Button Name="bt" Click="Button_Click_1">Click</Button>  


示例代码来自:msdn