2

I have a list view with multiple cells that has been populated using grid view. I want to add a text box and button to each row dynamically. How can i do it?

My try:

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

I am getting :

enter image description here

I want text box and button in column 3 and column 4.

Please help. I found a similar problem but cannot able to solve. Please help.

1
  • What you are doing there is adding TextBox as an Item, you need to set CellTemplate of the GridViewColumn as a TextBox Commented May 20, 2016 at 13:31

1 Answer 1

1

What you need to do is to create a CellTemplate for your GridView:
XAML (for the sake of my nick name)

<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       

With this as your 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>  


Example code from: msdn

Sign up to request clarification or add additional context in comments.

1 Comment

@XAMIMAX Thanks for the answer. I used DataTemplate and FrameworkElementFactory combination to achieve the desired result. Thanks for your effort and marking it as an answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.