相关源FindAncestor WPF 4& amp; Silverlight 5 [英] RelativeSource FindAncestor Differences in WPF 4 & Silverlight 5

查看:42
本文介绍了相关源FindAncestor WPF 4& amp; Silverlight 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   TextBox     Horizo​​ntalAlignment   =    高度  =  23    TextWrapping   = 换行   正文  =  TextBox1    VerticalAlignment   =  Top   宽度  =  120 >  
< interactiveness:Interaction.Triggers >
< interactive:EventTrigger EventName = GotFocus >
< interactive:InvokeCommandAction 命令 = {Binding GotFocusCommand} CommandParameter = {Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = TextBox}} / >
< / interactive:EventTrigger >
< / interactive:Interaction.Triggers >
< / TextBox >





在WPF 4中,CommandParameter正确设置为聚焦文本框。但是在SL5中,CommandParameter被设置为null。



但以下工作在WPF 4& SL5:



 <   TextBox     Horizo​​ntalAlignment   =    高度  =  23    TextWrapping   = 换行   文字  =  TextBox1    VerticalAlignment   =  Top   宽度  =  120 >  
< interactive:Interaction.Triggers >
< interactive:EventTrigger EventName = GotFocus >
< 交互性:InvokeCommandAction 命令 = {Binding GotFocusCommand} CommandParameter = {Binding Path = Children [0],RelativeSource = {RelativeSource FindAncestor,AncestorType = StackPanel}} / >
< / interactive:EventTrigger >
< / interactivity:Interaction.Triggers >
< / TextBox > ;









我更喜欢第一种方法我想把它放在一个风格和将它应用于TextBox-es跨应用程序中的许多xaml-s。

解决方案

Hi Padmanabhan,Silverlight不支持AncestorType。或者,您可以使用附加属性来模拟缺少的功能。



请访问此链接 [ ^ ];)



问候。

<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:

<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.

解决方案

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

Please visit this link[^] ;)

Regards.


这篇关于相关源FindAncestor WPF 4&amp; amp; Silverlight 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆