绑定到按钮的命令会被触发两次

时间:2016-02-29 13:21:53

标签: c# wpf binding command

我的WPF应用程序的用户可能会不时发出两次绑定到按钮的命令。

XAML代码:

   <Button x:Name="btnAccept" 
           Style="{StaticResource FlatButtonLarge}"
           Height="42" 
           Command="{Binding Path=SubmitCmd}"
           Content="Submit" />

我还有KeyBindings

<Window.InputBindings>
    <KeyBinding Key="F9" Command="{Binding SubmitCmd}" />
</Window.InputBindings>

我无法重现错误,但根据数据库中的更改,我得出结论,命令一次又一次被触发两次。它是否真的可行,我该如何防止这种现象。 SubmitCmd将新记录添加到数据库并关闭表单。

这是背后的代码:

    vm.SubmitCmd = new RelayCommand(pars => DoSubmit(), pars => vm.CmdSubmitCanExecute, "Submit" );

    private void DoSubmit()
    {
        try
        {
            if (!vm.LaunchAllowed)
            {
                this.Close();
            }
            else
            {
                vm.LaunchAllowed = false;
                bool isOk = DBService.SaveToDB(vm.Dto);

                if (isOk)
                {
                    DialogResult = true;
                    this.Close();
                }
                else
                {
                    ShowError(this, result);
                    vm.LaunchAllowed = true;
                }
            }
        }
        catch (Exception ex)
        {
            ShowError(this, ex.Message);
            vm.LaunchAllowed = true;
        }
    }

ViewModel代码:

    public ICommand SubmitCmd{ get; set; }

    public bool CmdSubmitCanExecute
    {
        get
        {
            return LaunchAllowed;
        }
    }

2 个答案:

答案 0 :(得分:0)

我想我也看到了这一点,但是 - 就像你一样 - 我无法重现它。这也是我的结论。

要解决此问题,我在命令运行后立即禁用了该按钮。因此,假设您使用mvvm,向其添加属性(不要忘记引发属性更改事件)并将按钮的IsEnabled-Property绑定到新属性

答案 1 :(得分:0)

我有同样的问题。 在我的情况下,我将一个命令绑定到两个组件中,这是错误的代码

<pv:PancakeView Grid.Row="3" CornerRadius="30" Margin="24,8,24,16" BackgroundColor="{StaticResource MainGreen}" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" HasShadow="True"
                 xct:TouchEffect.PressedOpacity="0.7"
                 xct:TouchEffect.NormalOpacity="1"
                 xct:TouchEffect.AnimationEasing="{x:Static Easing.CubicInOut}"
                 xct:TouchEffect.AnimationDuration="100"
                 xct:TouchEffect.PressedScale="0.9"
                 xct:TouchEffect.Command="{Binding Checkout}">
    <Label Text="CHECKOUT" TextColor="{StaticResource MainWhite}" FontSize="18" FontFamily="{StaticResource PoppinsMedium}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
    <pv:PancakeView.Shadow>
        <pv:DropShadow Offset="0,0" Opacity="0.15" BlurRadius="25"></pv:DropShadow>
    </pv:PancakeView.Shadow>
    <pv:PancakeView.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding Checkout}"/>
    </pv:PancakeView.GestureRecognizers>
</pv:PancakeView>

在那个 xaml 代码中,我将 Checkout 命令绑定到 TapGestureRecognizerxct:TouchEffect

删除其中一个为我解决问题。

对于您的情况,我认为以下方法可行

在 KeyBindings 区域将绑定的命令更改为新命令,例如 <KeyBinding Key="F9" Command="{Binding OtherSubmitCmd}" />

public ICommand OtherSubmitCmd{ get; set; } 添加到您的 ViewModel。然后为 OtherSubmitCmd 分配相同的方法。