Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将控件作为参数传递给委托_C#_Delegates_Code Behind - Fatal编程技术网

C# 如何将控件作为参数传递给委托

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设置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
有人能帮我指出我做错了什么吗

非常感谢

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(…)
与委托相关,而不是与方法相关。