c# - WPF按钮单击和命令不能一起工作MVVM

标签 c# wpf mvvm

我正在使用 mvvm 开发一些 wpf 应用程序。我正在尝试同时使用按钮单击事件和命令,但命令永远不会执行。此外,当我只使用没有点击事件的命令时,它工作得很好。这是代码:

<ControlTemplate x:Key="reminderDataTemplate">          
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25" />
            <RowDefinition Height="150" />
            <RowDefinition Height="20" />
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="New reminder:" HorizontalAlignment="Left" />
        <TextBox Text="{Binding Path=ReminderText, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Height="150" Width="200" HorizontalAlignment="Left"/>
        <Button Name="btnSaveReminder" Grid.Row="2" Content="Save" Width="auto" Height="20" HorizontalAlignment="Left" Click="btnSaveReminder_Click"  Command="{Binding Path= btnSaveReminder}"  />
    </Grid>
</ControlTemplate>

为什么会这样?

只是提一下,我必须同时使用单击和命令,因为我的 View 和 View 模型在不同的项目中。

更新: 还要说的是,当我在控件模板之外的按钮中同时使用单击和命令时,一切都很完美。

最佳答案

Click 事件 上,执行命令。

private void btnClick(object sender, RoutedEventArgs e)
{
    var btn = sender as Button;
    btn.Command.Execute(btn.CommandParameter);
}

关于c# - WPF按钮单击和命令不能一起工作MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20565719/

相关文章:

ios - SwiftUI MVVM 的参数 JSONDecoder URLSession 缺少参数

ios - 使用 ReactiveCocoa 观察 NSArray 中的对象变化

c# - 使用泛型可以将提供的类型限制为内置类型吗?

c# - 如何还原对其 MDF 和 LDF 文件使用相同命名方案的多个 SQL Server 数据库

c# - 无法捕获 Winforms 中未处理的异常

c# - WPF, EF : Show and save ordering of rows in datagrid, mvvm

c# - Dispatcher.BeginInvoke 始终返回 DispatcherOperationStatus.Pending 状态

c# - 在 WPF 中处理文本框上的 Enter 事件时,如何接受 Ctrl+Enter 作为返回?

WPF 基于 ListView 项目过滤组合框项目

c# - PagedList与ViewModels和.Skip()。Take()一起使用