WPF - 未调用命令绑定
WPF - command binding not being called
我有一个关闭按钮,它绑定到我的主视图模型中定义的关闭命令,但由于某种原因它没有触发:
mainview.xaml中的按钮:
<Button Grid.Row="0" Style="{DynamicResource MyButtonStyle}" Margin="270,3,10,7"
Command="{Binding CloseCommand}"/>
MainViewModel 中的命令声明:
public class MainViewModel : BaseViewModel
{
public ICommand CloseCommand { get; set; }
}
CloseCommand.cs中的命令定义:
public class CloseCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
System.Windows.Application.Current.Shutdown();
}
}
我在 CloseCommand 设置了一个断点,但它甚至没有到达那里,这是怎么回事?
我认为Rufo爵士指出了您的问题。但我想推荐你看看Community Toolkit MVVM。它的源代码生成器将帮助您处理 MVVM 相关代码。
例如,您的 ViewModel...
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace WpfCloseCommandSample;
[ObservableObject]
// This class needs to be "partial" for the source generator.
public partial class MainWindowViewModel
{
[ObservableProperty]
// The source generator will create a
// "ButtonName" property for you.
private string _buttonName;
[ICommand]
// The source generator will create a
// "CloseCommand" command for you.
private void Close()
{
System.Windows.Application.Current.Shutdown();
}
// Constructor
public MainWindowViewModel()
{
this.ButtonName = "Click here to close the window";
}
}
还有你的XAML...
<Window
x:Class="WpfCloseCommandSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfCloseCommandSample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Button Command="{Binding CloseCommand}" Content="{Binding ButtonName}" />
</Grid>
</Window>
我有一个关闭按钮,它绑定到我的主视图模型中定义的关闭命令,但由于某种原因它没有触发:
mainview.xaml中的按钮:
<Button Grid.Row="0" Style="{DynamicResource MyButtonStyle}" Margin="270,3,10,7"
Command="{Binding CloseCommand}"/>
MainViewModel 中的命令声明:
public class MainViewModel : BaseViewModel
{
public ICommand CloseCommand { get; set; }
}
CloseCommand.cs中的命令定义:
public class CloseCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
System.Windows.Application.Current.Shutdown();
}
}
我在 CloseCommand 设置了一个断点,但它甚至没有到达那里,这是怎么回事?
我认为Rufo爵士指出了您的问题。但我想推荐你看看Community Toolkit MVVM。它的源代码生成器将帮助您处理 MVVM 相关代码。
例如,您的 ViewModel...
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace WpfCloseCommandSample;
[ObservableObject]
// This class needs to be "partial" for the source generator.
public partial class MainWindowViewModel
{
[ObservableProperty]
// The source generator will create a
// "ButtonName" property for you.
private string _buttonName;
[ICommand]
// The source generator will create a
// "CloseCommand" command for you.
private void Close()
{
System.Windows.Application.Current.Shutdown();
}
// Constructor
public MainWindowViewModel()
{
this.ButtonName = "Click here to close the window";
}
}
还有你的XAML...
<Window
x:Class="WpfCloseCommandSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfCloseCommandSample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Button Command="{Binding CloseCommand}" Content="{Binding ButtonName}" />
</Grid>
</Window>