2012-11-01 18 views
0

我正在創建一個程序,其中用戶正在創建一個操作列表。可能的操作都具有需要指定的多個屬性。我想在列表框中顯示這些屬性,類似於Visual Studio在設計模式下顯示對象屬性的方式。字符串獲取文本框,布爾獲取複選框等。我已經想出瞭如何在列表中顯示對象的成員,但我正在努力想出如何爲每個控件創建回調。繼承人的基本結構:爲WPF/C中的多個對象創建Visual Studio屬性窗口#

void createPropertyList(object move) 
    { 
     Type type = move.GetType(); 
     FieldInfo[] fields = type.GetFields(); 
     propertyListBox.Items.Clear(); 

     foreach (var field in fields) 
     { 
      string name = field.Name; 
      object temp = field.GetValue(move); 

      if (temp is double) 
      { 
       double value = (double)temp; 
       Label l = new Label(); 
       l.Content = name; 

       TextBox t = new TextBox(); 
       t.Text = value.ToString("0.0000");      

       // put it in a grid, format it, blah blah blah  

       propertyListBox.Items.Add(grid); 
      } 

      // reflect on other types 
     } 
    } 

我假設有將是涉及一個lambda,但我怎麼回覆的字段信息數組實際引用的成員,所以我可以把用戶的輸入回對象?

回答

1

對不起,我應該看看FieldInfo。 GetValue有一個對應的SetValue。奇蹟般有效。

0

您可能會考慮使用Binding和DependencyPropertys綁定各種控件。它是一個更WPF的方式來做到這一點。

0

您需要爲所有類型的控件創建狀態更改事件處理程序。

例如文本框

... 
TextBox t = new TextBox(); 
t.Tag=name; 
t.KeyUp+=t_KeyUp; 

其中

private void t_KeyUp(object sender, KeyEventArgs e) 
{ 
    TextBox source = (sender as TextBox); 
    MessageBox.Show("Field "+source.Tag + " is changed. New value is "+source.Text); 
} 
相關問題