I am trying to call a selected listbox item from a button, not the listbox.selecteditemchanged
method in wpf. So when i try
string yadda = listbox.SelectedItem.ToString();
i get an exception:
The calling thread cannot access this object because a different thread owns it.
So, what i was trying to do is the following:
Dispatcher.BeginInvoke(() =>
{
lbxSelectedItem = (lbxFileList.SelectedItem as TextBlock).Text;
});
That is not working either because i get another exception:
Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type
Convert the lambda to an Action
:
Dispatcher.BeginInvoke((Action)(() => { /*DoStuff*/ }));
Or construct one from the lambda:
Dispatcher.BeginInvoke(new Action(() => { /*DoStuff*/ }));
You probably can write an extension method for the Dispatcher
that takes an Action
, that way the lambda would be implicitly converted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With