2016-06-07 81 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) –