c# - WPF MVVM Interaction Binding CommandParameter to UI element -


i want text in textbox selected when textbox gets focused. therefore need binding command "gotfocus" event. special thing is, textbox created dynamically via itemscontrol. there binding usercontrol (view), itemscontrol , item itself. when tried bind ui element commandparameter got model bindet current item in itemscontrol.

all bindings working except commandparameter..

somebody got idea how working?

here code:

xaml

/////// <usercontrol/> information: xmlns:i="clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity" x:name="mainbindingcontrol" ///////      <itemscontrol itemssource="{binding mysecondmodellist }" margin="10,10,10,0">         <itemscontrol.itemspanel>             <itemspaneltemplate>                 <grid helper:gridhelper.rowcount="{binding myfirstmodel.rows}" helper:gridhelper.columncount="{binding myfirstmodel.columns}">                 </grid>             </itemspaneltemplate>         </itemscontrol.itemspanel>         <itemscontrol.itemcontainerstyle>             <style>                 <setter property="grid.row" value="{binding row}" />                 <setter property="grid.column" value="{binding column}" />             </style>         </itemscontrol.itemcontainerstyle>         <itemscontrol.itemtemplate>             <datatemplate>                 <textbox margin="25,25,25,25" height="30" width="30" text="{binding text, mode=twoway, updatesourcetrigger=lostfocus}" textalignment="center" verticalcontentalignment="center" >                     <i:interaction.triggers>                         <i:eventtrigger eventname="gotfocus">                             <i:invokecommandaction command="{binding elementname=mainbindingcontrol, path=datacontext.textboxfocuscommand}" commandparameter="{binding relativesource={ relativesource self }}"/>                         </i:eventtrigger>                     </i:interaction.triggers>                 <textbox.style>                     <style targettype="textbox">                         <setter property="background" value="orangered" />                             <style.triggers>                                 <trigger property="text" value="0">                                     <setter property="background" value="orange" />                                 </trigger>                                 <trigger property="text" value="1">                                     <setter property="background" value="white" />                                 </trigger>                                 <trigger property="text" value="2">                                     <setter property="background" value="white" />                                 </trigger>                                 <trigger property="text" value="3">                                     <setter property="background" value="white" />                                 </trigger>                                 <trigger property="text" value="4">                                     <setter property="background" value="white" />                                 </trigger>                             </style.triggers>                         </style>                     </textbox.style>                 </textbox>             </datatemplate>         </itemscontrol.itemtemplate>     </itemscontrol> 

cs

    #region textboxfocus     private icommand _textboxfocuscommand;     public icommand textboxfocuscommand     {         { return _textboxfocuscommand; }         set { _textboxfocuscommand = value; }     }     public void textboxfocus(object parameter)     {         var _tmp = parameter textbox;         if (_tmp != null )         {             _tmp.selectall();         }     }     #endregion 

models

public class firstmodel     {         public int rows { get; set; }         public int columns { get; set; }     }  public class secondmodel     {         public int row { get; set; }         public int column { get; set; }         public string text { get; set; }     }  public class viewmodel     {         public firstmodel myfirstmodel { get; set; }         public observablecollection<secondmodel> mysecondmodellist { get; set; }     } 

since want related view, i'd add code in code-behind, instead of trying use commands , textbox inside viewmodel. in mvvm should never reference ui assemblies viewmodel. ok use code-behind if trying related view.

so, inside style of textbox, have:

<eventsetter event="gotfocus" handler="textbox_gotfocus"/> 

and in code-behind of usercontrol:

private void textbox_gotfocus(object sender, routedeventargs e) {     textbox textbox = sender textbox;     textbox.selectall(); } 

the complete code of datatemplate be:

<datatemplate>     <textbox margin="25,25,25,25" height="30" width="30" text="{binding text, mode=twoway, updatesourcetrigger=lostfocus}" textalignment="center" verticalcontentalignment="center" >         <!-- erase block of code         <i:interaction.triggers>             <i:eventtrigger eventname="gotfocus">                 <i:invokecommandaction command="{binding elementname=mainbindingcontrol, path=datacontext.textboxfocuscommand}" commandparameter="{binding relativesource={ relativesource self }}"/>             </i:eventtrigger>         </i:interaction.triggers>-->         <textbox.style>                                         <style targettype="textbox">                 <eventsetter event="gotfocus" handler="textbox_gotfocus"/>                 <setter property="background" value="orangered" />                 <style.triggers>                     <trigger property="text" value="0">                         <setter property="background" value="orange" />                     </trigger>                     <trigger property="text" value="1">                         <setter property="background" value="white" />                     </trigger>                     <trigger property="text" value="2">                         <setter property="background" value="white" />                     </trigger>                     <trigger property="text" value="3">                         <setter property="background" value="white" />                     </trigger>                     <trigger property="text" value="4">                         <setter property="background" value="white" />                     </trigger>                 </style.triggers>             </style>         </textbox.style>     </textbox> </datatemplate> 

notice method selectall() of textbox called on gotfocus event has little trick work intended. check question: how automatically select text on focus in wpf textbox?


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -