How to return a value with Dispatcher.Invoke?

后端 未结 5 1979
执念已碎
执念已碎 2020-12-20 10:42

Anyone knows how to return a value from Dispatcher.Invoke in wpf? I want to return the selected index for a ComboBox.

Thanks!

5条回答
  •  -上瘾入骨i
    2020-12-20 11:43

    This is my method to retrieve selected value for a combobox, how can I say delegate to return value?

        private object getValueCB(System.Windows.Controls.ComboBox cb)
        {
            object obj;
    
    
                if (!cb.Dispatcher.CheckAccess())
                {
                    obj = cb.Dispatcher.Invoke(
                      System.Windows.Threading.DispatcherPriority.Normal,
                      new Action(
                        delegate()
                        {
                            obj = cb.SelectedValue;
                        }
                    ));
    
                    return obj;
                }
                else
                {
                    return obj = cb.SelectedValue;
                }
    
        }
    

提交回复
热议问题