wpf - WPF:实现 MVVM 按钮命令
问题描述
所以我有WPF
主windoes和2的应用程序UserControls
:
HomeView.xaml
OptionsView.xaml
查看模型
public class ApplicationViewModel : INotifyPropertyChanged
{
#region Fields
private ICommand changePageCommand;
private ICommand addFilesCommand;
private IPageViewModel _currentPageViewModel;
private List<IPageViewModel> _pageViewModels;
#endregion
public ApplicationViewModel()
{
// Add available pages
PageViewModels.Add(new HomeViewModel() { IsSelected = true });
PageViewModels.Add(new OptionsViewModel() { IsSelected = false });
// Set starting page
CurrentPageViewModel = PageViewModels[0];
}
#region Properties / Commands
public ICommand ChangePageCommand
{
get
{
if (changePageCommand == null)
{
changePageCommand = new RelayCommand(
p => ChangeViewModel((IPageViewModel)p),
p => p is IPageViewModel);
}
return changePageCommand;
}
}
public List<IPageViewModel> PageViewModels
{
get
{
if (_pageViewModels == null)
_pageViewModels = new List<IPageViewModel>();
return _pageViewModels;
}
}
public IPageViewModel CurrentPageViewModel
{
get
{
return _currentPageViewModel;
}
set
{
if (_currentPageViewModel != value)
{
_currentPageViewModel = value;
OnPropertyChanged("CurrentPageViewModel");
}
}
}
#endregion
#region Methods
private void ChangeViewModel(IPageViewModel viewModel)
{
if (!PageViewModels.Contains(viewModel))
PageViewModels.Add(viewModel);
CurrentPageViewModel = PageViewModels.FirstOrDefault(vm => vm == viewModel);
}
#endregion
}
当应用程序开始
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow app = new MainWindow();
ApplicationViewModel context = new ApplicationViewModel();
app.DataContext = context;
app.Show();
}
}
视窗资源
<Window.Resources>
<DataTemplate DataType="{x:Type home:HomeViewModel}">
<home:HomeView />
</DataTemplate>
<DataTemplate DataType="{x:Type options:OptionsViewModel}">
<options:OptionsView />
</DataTemplate>
</Window.Resources>
在里面HomeView.xaml
我有简单的button
:
<Button Command="{Binding DataContext.AddFilesCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
CommandParameter="{Binding}"/>
我想添加简单的Click
命令,一些东西。
所以我尝试添加这个ICommand
:
public ICommand AddFilesCommand
{
}
有什么建议如何在每个之后执行的命令上添加这种类型Button Click
?
解决方案
这可以更容易地完成。我会创建一个类来轻松实现命令:
using System;
using System.Windows.Input;
namespace YourNameSpace
{
public class RelayCommand : ICommand
{
private Action Action;
public Command(Action _action)
{
Action = _action;
}
public event EventHandler CanExecuteChanged = (sender, e) => { };
public bool CanExecute(object parameter) => true;
public void Execute(object parameter)
{
Action();
}
}
}
然后创建命令(你不需要私有 ICommand ):
public ICommand AddFileCommand { get; set; }
并像这样使用它(在构造函数中):
AddFileCommand = new RelayCommand(()=>
{
MethodToExecute();
});
XAML:
<Button Command="{Binding AddFileCommand}"/>
这样,您的代码将更容易看到低谷。
推荐阅读
- r - 在 R 中,从列表列表中提取具有相同名称的列表
- python - 从 tkinter 中的按钮调用命令时停止列表相乘
- python-3.x - 索引超出范围后如何返回值
- python - Python 请求 - 使用分页获取 json 数据
- microsoft-graph-api - 如何获取在线会议的通讯呼叫 ID?
- reactjs - 用于 API 函数的 useEffect 或 useMemo?
- azure-data-explorer - KQL 用于在 Azure 门户中查找过去 90 天未登录的非活动用户
- java - 有没有办法可以在我的 Java 程序中转换变量,以便获得双重输出?
- .htaccess - .htaccess 禁止通过不带扩展名的路径访问文件
- javascript - :class="`level ${file.invalidMessage && 'has-text-danger'}`" 是如何崩溃的?