Search code examples
c#delegatescode-behind

How to pass a control as a parameter to delegate


I am needing to style a ComboBoxItem for a ComboBox that is being created in code behind. Here's my code so far

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;
}

I am getting the following errors

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

Please can someone help me in pointing as to what I am doing wrong?

Many Thanks


Solution

  • Try using either of these:

    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);