2011-10-18 73 views
0

我需要爲在後面的代碼中創建的ComboBox設計ComboBoxItem風格。這裏是我到目前爲止的代碼如何將控件作爲參數傳遞給委託

ComboBox cbo1 = new ComboBox();     
cbo1.IsTextSearchEnabled = true; 
cbo1.IsEditable = true; 

grid1.Children.Add(cbo1); 

cbo1.Dispatcher.BeginInvoke(new StyleComboBoxItemDelegate(ref StyleComboBoxItem(cbo1), System.Windows.Threading.DispatcherPriority.Background); 

public delegate void StyleComboBoxItemDelegate(ComboBox cbo_tostyle); 

public void StyleComboBoxItem(ComboBox cbo_tostyle) 
{ 
//code to style the comboboxitem; 
} 

我收到以下錯誤

1. A ref or out argument must be an assignable variable 
2. Method name expected 

請有人可以幫我指點,什麼我做錯了嗎?

非常感謝

回答

1

嘗試使用以下任一:

cbo1.Dispatcher.BeginInvoke(
    (Action)(() => StyleComboBoxItem(cbo1)), 
    System.Windows.Threading.DispatcherPriority.Background); 

cbo1.Dispatcher.BeginInvoke(
    (Action)(() => 
    { 
     //code to style the comboboxitem; 
    }), 
    System.Windows.Threading.DispatcherPriority.Background); 
+0

超酷:),非常感謝。 –

1

StyleComboBoxItem()「返回」無效,所以通過使用ref StyleComboBoxItem(...)你實際上是試圖建立一個引用無效。

你既可以:

  • 風格在單獨的行組合框,然後提供樣式組合框的委託
  • StyleComboBoxItem()返回它的風格的組合框,所以你仍然可以使用它內嵌

該ref是不需要的。

+0

的'REF StyleComboBoxItem(...)'涉及委託,而不是方法。 – Enigmativity