>>>>>Hi all,
>>>>>
>>>>>Is there any way to set the focus to an WPF UserControl´s Element programaticaly?
>>>>>
>>>>>I can SetFocus to the UserControl, but how be set focus to one of its elements?
>>>>>
>>>>>Thanks in advance,
>>>>>A. Llano
>>>>
>>>>My focus statements ussually end up looking like this:
>>>>
>>>>
>>>>Dispatcher.BeginInvoke(DispatcherPriority.Input,
>>>> new ThreadStart(delegate()
>>>> {
>>>> myUserControl.txtObject.Focus();
>>>> }));
>>>>
>>>I came across this construct (on a SilverLight issue):
Dispatcher.BeginInvoke(() =>
>>>{
>>>myUserControl.txtObject.Focus();
>>>}
which looks like the same idea but just queuing on the same thread?
>>>
>>>UPDATE: Just tried it and it doesn't compile anyway :-{ - but a modified version of your code using the same thread works OK....
>>>
>>>UPDATE 2: This works:
Dispatcher.BeginInvoke(new Action(()=>myUserControl.txtObject.Focus();));
>>
>>"Just tried it and it doesn't compile"
>>
>>Are you sure you had all the usings in?
>>
>>I pulled that from working code.
>
>Sorry, I should have been clearer. Your code was fine. The one that didn't work was the first sample I showed using a lambda expression. The last example (using Action()) also works. But I'm still not clear on the advantage of using a different thread?
>Regards,
>Viv
I'm pretty sure I originally got that code from Marco over on the MSDN boards. It's always worked great the way it is, so I've never really questioned it. I have also seen that exact same technique used many times in many programs. I tend to not mess with things too much when they work.
I've only touched the surface on the .NET threading and delegate stuff. But I'm also curious as to the advantages of the one method over the other here.
Since they work, both delegates must end up running in the same thread as the interface or they would fail.
Hmmm.... I wonder if that's why they do it with threadstart.
Sometimes when I create this I am not on the same thread as my interface. I suspect Action might fail in those cases?