如何在mvvm wpf中使用事件触发器

时间:2019-03-28 13:29:08

标签: c# wpf mvvm

当我在文本框中键入内容时,我想提高事件的击键强度,我的命令将只计算写入输入所花费的时间

我的xaml与文本框:

<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Texts.mydata,UpdateSourceTrigger=PropertyChanged}" FontWeight="SemiBold">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyDown">
            <cmd:EventToCommand Command="{Binding LigneAddCommand}" PassEventArgsToCommand="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBlock>

在我的视图模型中 这是我的relayCommand:

private RelayCommand _ligneAddCommand;
public RelayCommand LigneAddCommand => _ligneAddCommand = _ligneAddCommand ?? new RelayCommand(LigneAddKeyDownEventHandle, LignaAddCanBeExecuted);

protected virtual bool LignaAddCanBeExecuted() => true;

protected virtual void LigneAddKeyDownEventHandle()
{
    if (timer != null)
        timer.Stop();

    timer = new System.Windows.Threading.DispatcherTimer();
    timer.Tick += (s, args) =>
    {
        //fetch data here...
        timer.Stop();
    };
    timer.Interval = TimeSpan.FromMilliseconds(50);
    timer.Start();
}

我的问题是,当我在视图模型中放置断点时,似乎事件从未发生

0 个答案:

没有答案