title: C# Displaying text box (present in the list view) value in a message box (wpf)
body: <p>I have a list view in which each row contains 5 entries. The list box looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/73SS7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/73SS7.png" alt="!["></a></p>
  
  <p>I need to store (display) the name of variable and the value present in "Physical value" column text box when i press OK button. For e.g. if i enter 45 in the physical value text box (only one row at a time) then the name of the variable and value "45" should be stored (displayed). I am able to retrieve the name of the variables but not the value of the text box. </p>
  
  <p>My try:</p>
  
  <p>This code will populate the list view with variables and bind it to the properties.</p>
  
  <pre><code> public void Populatevariables(IList&lt;string&gt; variables)
      {
          int rowcount = 0;
          dataGrid.RowDefinitions.Clear();
          dataGrid.ColumnDefinitions.Clear();
  
          RowDefinition rd = new RowDefinition();
          rd.Height = new GridLength();
          dataGrid.RowDefinitions.Add(rd);
          dataGrid.RowDefinitions.Add(new RowDefinition());
          dataGrid.ColumnDefinitions.Add(new ColumnDefinition());
  
          Label t1 = new Label();
          t1.Content = "Variables";
          Grid.SetColumn(t1, 0);
          Grid.SetRow(t1, rowcount);
          dataGrid.Children.Add(t1);
  
  
  
          ListView VrblPopulateList = new ListView();
  
          GridView g1 = new GridView();
  
          g1.AllowsColumnReorder = true;
  
          //l1.View = g1;
          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 = "Current Value";
          g3.Width = 200;
  
          DataTemplate dt1 = new DataTemplate();
          FrameworkElementFactory FF1 = new FrameworkElementFactory(typeof(TextBox));
          FF1.SetBinding(TextBox.BindingGroupProperty, new Binding("Current_Value"));
          FF1.SetValue(FrameworkElement.HeightProperty, Height = 30);
          FF1.SetValue(FrameworkElement.WidthProperty, Width = 150);
          dt1.VisualTree = FF1;
          g3.CellTemplate = dt1;
  
  
          g1.Columns.Add(g3);
  
          GridViewColumn g6 = new GridViewColumn();
          g6.Header = "Physical Value";
          g6.Width = 200;
  
          DataTemplate dt2 = new DataTemplate();
          FrameworkElementFactory FF2 = new FrameworkElementFactory(typeof(TextBox));
          FF2.SetBinding(TextBox.BindingGroupProperty, new Binding("Physical_Value"));
          //FF2.AddHandler(TextBox.TextChangedEvent, txtchanged, true);
          FF2.SetValue(FrameworkElement.HeightProperty, Height = 30);
          FF2.SetValue(FrameworkElement.WidthProperty, Width = 150);
          dt2.VisualTree = FF2;
          g6.CellTemplate = dt2;
  
  
          g1.Columns.Add(g6);
  
  
          GridViewColumn g4 = new GridViewColumn();
          g4.Header = "Action";
          g4.Width = 200;
  
          DataTemplate dt = new DataTemplate();
          FrameworkElementFactory FF = new FrameworkElementFactory(typeof(Button));
          FF.SetBinding(Button.BindingGroupProperty, new Binding("ToDo"));
          FF.SetValue(FrameworkElement.HeightProperty,Height = 30);
          FF.SetValue(FrameworkElement.WidthProperty, Width = 150);
          FF.SetValue(System.Windows.Controls.Button.ContentProperty,"OK");
          FF.AddHandler(Button.ClickEvent, new RoutedEventHandler(b1_click));
          dt.VisualTree = FF;           
          g4.CellTemplate = dt;
  
          g1.Columns.Add(g4);
          VrblPopulateList.View = g1;
  
  
          Grid.SetRow(VrblPopulateList, rowcount + 1);
          dataGrid.Children.Add(VrblPopulateList);
  
  
          for (int i = 0; i &lt; variables.Count; i++)
          {
              Label lb1 = new Label();
              lb1.Content = variables[i].Name;
  
              Label lb2 = new Label();
              lb2.Name = variables[i].datatype;
  
              DataTemplate dd = new DataTemplate();
              TextBox tb = new TextBox();
  
              tb.Name = "TextBox" + i.ToString();
              Button b1 = new Button();
  
  
  
              VrblPopulateList.Items.Add(new User() { Name = lb1.Content, DataType = lb2.Name, Current_Value = tb, Physical_Value = tb, ToDo = b1 });
  
  
  
  
  
          }
  
  
  
      }
  </code></pre>
  
  <p>This code defines the property which is bind while populating:</p>
  
  <pre><code> public class User
     {
         public object Name { get; set; }
  
         public string DataType { get; set; }
  
         public Control Current_Value
         {
             get;
  
             set;
  
         }
  
         public Control Physical_Value
         {
             get;
  
             set;
  
         }
  
         public Control ToDo { get; set; }
  
     }
  </code></pre>
  
  <p><strong>At last this code will retrieve all the items when button is clicked.</strong> </p>
  
  <pre><code> private void b1_click(object sender, RoutedEventArgs e)
      {
  
          User item = (User)((Button)sender).DataContext;
  
          TextBox t = (TextBox)item.Physical_Value;
  
          MessageBox.Show(item.Name.ToString() + t.Text);
  
  
  
      }
  </code></pre>
  
  <p>The text box value is always empty. I know it can be solved by adding handler to "text changed" event while populating. But i dont know how to do it. Please help.</p>