开发者

Get and Setfocus of an element

开发者 https://www.devze.com 2023-04-08 15:11 出处:网络
What I am trying to do is get the last focus element before doing a refresh on my BO and set it back to the last focus value.

What I am trying to do is get the last focus element before doing a refresh on my BO and set it back to the last focus value.

the idea is something like below:

//get las开发者_运维技巧t focus element
var x = Keyboard.FocusedElement;
//refresh my businessobject here
CallMethod();
//set the focus to my last element
FocusManager.SetFocusedElement(focusScope, x);

... but the problem here is after calling the refresh object method my "x" value got changed in the process.

Anyone experience this problem?


You cannot just assign Keyboard.FocusedElement because Keyboard.FocusedElement is a reference type. So as soon as the Framework changes the Keyboard.FocusedElement, your copy changes. You will need to find a new way to identify the control that had focus before the call or try and find a deep copy solution that works.


Why not use the Focus() method??

//get focused element
UIElement x = Keyboard.FocusedElement as UIElement;

//refresh my businessobject here
CallMethod();

//set the focus to my last element
x.Focus();

or consider using FocusManager.GetFocusedElement(focusScope)...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号