2016-06-07 75 views
0

我有一个列表视图,其中每行包含5个条目。列表框看起来是这样的:C#在消息框(wpf)中显示文本框(存在于列表视图中)值

![

我需要存储(显示)变量的名称,并出现在“体力值”栏中的文本框,当我按下OK键的值。对于例如如果我在物理值文本框中输入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); 



    } 

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

+1

您是否尝试过设置'PropertyChanged'绑定的'UpdateSourceTrigger'属性,并修改您的用户模型以代替具有Control的Physical_Value属性而是字符串(您将绑定到数据模板中的文本框的Text属性) ? –

+0

你的意思是? FF2.SetBinding(TextBox.BindingGroupProperty,new Binding(“Physical_Value”){UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged}); – sats

+0

是的,但你也应该改变你的模型。您的模型不应该包含控件。它应该包含xaml需要使用的控件值。我会建议你会在[本文]中做类似的事情(http://www.wpf-tutorial.com/datagrid-control/custom-columns/)。这会让你的代码更具可读性。 –

回答

0

为在@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。

+0

不要忘记为ToDo和Current_Value属性做同样的事情。而且您不必将PhysicalValue属性重命名为PhysicalValueTxtChanged。无论如何,我强烈建议你会读一些关于wpf xaml和绑定的内容。您应该将更多代码从代码移到xaml,然后在xaml中执行绑定。 无论如何,很高兴有帮助 –

+0

感谢您的建议:)。我会做更多的阅读。我只有一个月的wpf和绑定。 :( – sats

+0

这很好,在你将获得关于wpf的一些常识以及如何正确使用它之后,你也可以阅读关于棱镜以及如何在你的wpf应用程序中使用它。 ://github.com/PrismLibrary/Prism/tree/master/Documentation),当然还有一些[样本](https://github.com/PrismLibrary/Prism-Samples-Wpf) –