繁体   English   中英

C#在消息框(wpf)中显示文本框(在列表视图中显示)值

[英]C# Displaying text box (present in the list view) value in a message box (wpf)

我有一个列表视图,其中每行包含5个条目。 列表框如下所示:

![

当我按确定按钮时,我需要存储(显示)变量的名称和“物理值”列文本框中显示的值。 例如,如果我在物理值文本框中输入45(一次仅输入一行),则变量的名称和值“ 45”应存储(显示)。 我能够检索变量的名称,但不能检索文本框的值。

我的尝试:

此代码将使用变量填充列表视图,并将其绑定到属性。

 public void Populatevariables(IList<string> 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 < 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 });





        }



    }

这段代码定义了在填充时绑定的属性:

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

   }

最后,当单击按钮时,此代码将检索所有项目。

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



    }

文本框值始终为空。 我知道可以通过在填充时将处理程序添加到“文本更改”事件中来解决。 但是我不知道该怎么做。 请帮忙。

如@Ponas Justas的评论中所述,我必须对代码进行以下更改:

  1. 设置文本框的UpdateSourceTrigger属性。
  2. 相应地更改用户模型。

完成上述更改后,我的代码如下所示:

 FF2.SetBinding(TextBox.BindingGroupProperty, new Binding("Physical_Value"));
 FF2.SetBinding(TextBox.TextProperty, new Binding("PhysicalValueTxtChanged") { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });

用户模型

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

       public string DataType { get; set; }

       public Control Current_Value
       {
           get;

           set;

       }
       public string _PhysicalValueTxtChanged = null;
       public string PhysicalValueTxtChanged
       {
           get { return _PhysicalValueTxtChanged; }

           set
           {
               _PhysicalValueTxtChanged = value;
           }

       }

       public Control ToDo { get; set; }

   }

完成此操作后,只需修改即可轻松存储文本框中的文本:

 private void b1_click(object sender, RoutedEventArgs e)
    {

        User item = (User)((Button)sender).DataContext;

        string txt = item.PhysicalValueTxtChanged;

        MessageBox.Show(item.Name.ToString() + txt);


    }

非常感谢Ponas Justas。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM