Tuesday, 15 February 2011

c# - How to select all text in TextBoxs using Caliburn.Micro? -



c# - How to select all text in TextBoxs using Caliburn.Micro? -

in wpf application utilize caliburn.micro bind between view , viewmodel.

in view have stackpanel text box

<stackpanel> <textbox x:name="ctx_a" /> <textbox x:name="ctx_b" /> <textbox x:name="ctx_c" /> <textbox x:name="ctx_d" /> </stackpanel>

now want select text after focus on textbox. in viewmodel write method textboxgotfocus

public void textboxgotfocus(object sender) { var tb = sender textbox; if (tb != null) { tb.selectall(); } }

next go view , write that:

<stackpanel> <textbox x:name="ctx_a" cal:message.attach=" [event gotfocus]=[action textboxgotfocus($source)]"/> <textbox x:name="ctx_b" cal:message.attach=" [event gotfocus]=[action textboxgotfocus($source)]"/> <textbox x:name="ctx_c" cal:message.attach=" [event gotfocus]=[action textboxgotfocus($source)]"/> <textbox x:name="ctx_d" cal:message.attach=" [event gotfocus]=[action textboxgotfocus($source)]"/> </stackpanel>

is there possible attach method stackpanel instead textbox?

i remove textboxgotfocus() method view model, because it's blurring lines between view , view model. view models shouldn't using wpf controls in them. instead, easiest way can think of create class inherits textbox, , attach event that:

public class autofocustextbox : textbox { public autofocustextbox() { gotfocus += (sender, e) => selectall(); } }

then in xaml, can utilize this:

<local:autofocustextbox ... />

that maintain xaml lot cleaner

c# wpf caliburn.micro

No comments:

Post a Comment