EventToCommand对于初始GotFocus事件失败

时间:2011-11-09 06:09:59

标签: focus mvvm-light eventtocommand

我开始在我当前的项目中应用Mvvm设计模式,我使用的框架是Mvvm Light工具包。现在我在使用EventToCommand处理“GotFocus”事件时遇到了一个问题。 xaml文件类似于:

<TextBox x:Name="TextBox1">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="GotFocus">
            <cmd:EventToCommand Command="{Binding TestCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
 </TextBox>

每当触发“GotFocus”时,我想在视图模型中执行TestCommand。 但问题是初始“GotFocus”(即窗口加载时)没有执行“TestCommand”。我已经调试并发现“GotFocus”事件实际上已被触发,但触发器未因未知原因而被调用。   然后我将焦点设置在“Window.Loaded”事件处理程序中,它仍然失败。

protected void WindowLoaded(object sender, RoutedEventArgs e)
    {
        FocusManager.SetFocusedElement(this, TextBox1); // The focus is moved to TextBox1 but the associated command is not executed.
    }

但是如果我将焦点设置在“Window.Activated”事件处理程序中,那就没问题了。

    protected void WindowActivated(object sender, EventArgs e)
    {
        FocusManager.SetFocusedElement(this, TextBox1); // The focus is moved to TextBox1 and the command is executed.
    }

我对发生的事感到非常困惑。任何人都可以详细解释一下吗?

0 个答案:

没有答案