C# 如何将控件作为参数传递给委托
我需要为正在代码隐藏中创建的ComboBox设置ComboBoxItem的样式。这是到目前为止我的代码C# 如何将控件作为参数传递给委托,c#,delegates,code-behind,C#,Delegates,Code Behind,我需要为正在代码隐藏中创建的ComboBox设置ComboBoxItem的样式。这是到目前为止我的代码 ComboBox cbo1 = new ComboBox(); cbo1.IsTextSearchEnabled = true; cbo1.IsEditable = true; grid1.Children.Add(cbo1); cbo1.Dispatcher.BeginInvoke(new StyleComboBoxItemDelegate(ref St
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
有人能帮我指出我做错了什么吗
非常感谢stylecomboxItem()
返回“void”,因此通过使用ref stylecomboxItem(…)
您实际上是在尝试创建对void的引用
你可以:
- 在单独的行上设置组合框的样式,然后将已设置样式的组合框提供给代理
- 让
返回其样式化的组合框,这样您仍然可以内联使用它stylecomboxItem()
不需要ref。尝试使用以下任一选项:
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);
ref-stylecomboxItem(…)
与委托相关,而不是与方法相关。