wpf - Setting Focus on a Control Within a ControlTemplate (Part 2) -


i'm stumped on must surely 1 of common wpf requirements. i've read this question implementation of solution not work.

here's markup lookless control:

<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                 xmlns:local="clr-namespace:wpftest">   <style targettype="{x:type local:customcontrol}">     <setter property="template">       <setter.value>         <controltemplate targettype="{x:type local:customcontrol}">           <border>             <textbox x:name="mytextbox" />           </border>           <controltemplate.triggers>             <trigger property="isfocused"                      value="true">               <setter property="focusmanager.focusedelement"                       value="{binding elementname=mytextbox}" />               <setter targetname="mytextbox"                       property="background"                       value="green" />             </trigger>           </controltemplate.triggers>         </controltemplate>       </setter.value>     </setter>   </style> </resourcedictionary> 

here's markup window contains instance of customcontrol:

<window x:class="wpftest.window1"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:local="clr-namespace:wpftest"         title="window1" height="300" width="300">    <local:customcontrol x:name="cctl" /> </window> 

and here's code-behind:

public partial class window1 : window {     public window1()     {         initializecomponent();         loaded += (routedeventhandler)delegate { cctl.focus(); };     } } 

when window1 loaded, text box turns green (indicating trigger works) focus remains cctl , not text box. doubtless has output displaying following data error:

cannot find source binding reference 'elementname=mytextbox'. bindingexpression:(no path); dataitem=null; target element 'customcontrol' (name='cctl'); target property 'focusedelement' (type 'iinputelement').

i've no idea why error appearing. pointers gratefully received, thanks.

try using trigger instead:

<trigger property="isfocused" value="true">     <setter targetname="mytextbox" property="focusmanager.focusedelement" value="{binding elementname=mytextbox}" /> </trigger> 

the error telling can't locate mytextbox because name isn't in scope focusedelement property being applied. in case that's on cctl instance itself, can't see inside own template. setting property on inside template binding can locate named element.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -