2011-10-18 74 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