WinRT XAML如何在使用Win8nl EventToCommandBehaviour时获取文本框的当前文本

时间:2013-02-06 17:12:28

标签: winrt-xaml

我一直在努力解决这个问题,我很难过。

我有以下XAML:

<TextBox x:Name="MyTextBox" Text="{Binding MyName, Mode=TwoWay}" Width="200">
            <WinRtBehaviors:Interaction.Behaviors>
                <Win8nl_Behavior:EventToCommandBehavior Event="TextChanged"  
                                                        Command="TextChangedCommand" 
                                                        CommandParameter="{Binding Text, ElementName=MyTextBox, Mode=OneWay}"/>
            </WinRtBehaviors:Interaction.Behaviors>

        </TextBox>

在我的视图模型中:

        public ICommand TextChangedCommand
    {
        get
        {
            return new RelayCommand<string>((p) =>
            {
                var msg = new MessageDialog(string.Format("Hi there {0}", p));
                msg.ShowAsync();
            });
        }
    }

但我希望出现在CommanParameter(p)中的字符串值始终为null。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您可以做的最好的事情是将文本值作为CommandParameter传递。

答案 1 :(得分:1)

行为不是Visual Tree的一部分,因此它们无权访问XAML范围和执行ElementName绑定的功能。此blog post提供了更多详细信息和合适的解决方案。