Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox1" VerticalAlignment="Top" Width="120">
                <interactivity:Interaction.Triggers>
                    <interactivity:EventTrigger EventName="GotFocus">
                        <interactivity:InvokeCommandAction Command="{Binding GotFocusCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=TextBox}}" />
                    </interactivity:EventTrigger>
                </interactivity:Interaction.Triggers>
            </TextBox>


In WPF 4, the CommandParameter is correctly set to the focused Textbox. But in SL5, the CommandParameter is set to null.

But the following works in both WPF 4 & SL5:

XML
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox1" VerticalAlignment="Top" Width="120">
    <interactivity:Interaction.Triggers>
        <interactivity:EventTrigger EventName="GotFocus">
            <interactivity:InvokeCommandAction Command="{Binding GotFocusCommand}" CommandParameter="{Binding Path=Children[0], RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel}}" />
        </interactivity:EventTrigger>
    </interactivity:Interaction.Triggers>
</TextBox>




I prefer the first method as I want to put this in a style & apply it to TextBox-es across many xaml-s in an application.
Posted
Updated 20-Aug-13 18:54pm
v2

1 solution

Hi Padmanabhan, AncestorType isn't supported by Silverlight. Alternatively you can use Attached Properties to simulate that missing functionality.

Please visit this link[^] ;)

Regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900