[英]C# WPF MVVM Light CanExecute not recognized
我目前正在學習WPF和MVVM,並且有一些問題。
我正在使用MVVM Light,我希望在驗證后禁用/啟用某些按鈕,但不使用功能。
ViewModelMain:
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace EF_MVVM_Test
{
public class ViewModelMain : ViewModelBase
{
public RelayCommand AddAuthorCommand { get; private set; }
public RelayCommand DeleteAuthorCommand { get; private set; }
public RelayCommand RefreshCommand { get; private set; }
public RelayCommand SaveCommand { get; private set; }
public ObservableCollection<Author> Authors { get; set; }
private LibraryContext db;
private Author _SelectedAuthor;
public Author SelectedAuthor
{
get { return _SelectedAuthor; }
set { Set("SelectedAuthor", ref _SelectedAuthor, value); }
}
private Author _NewAuthor;
public Author NewAuthor
{
get { return _NewAuthor; }
set { Set("NewAuthor", ref _NewAuthor, value); }
}
public ViewModelMain()
{
db = new LibraryContext();
db.Author.Load();
Authors = db.Author.Local;
AddAuthorCommand = new RelayCommand(ExecuteAddAuthorCommand, CanExecuteAddAuthorCommand);
DeleteAuthorCommand = new RelayCommand(ExecuteDeleteAuthorCommand, CanExecuteDeleteAuthorCommand);
RefreshCommand = new RelayCommand(ExecuteRefreshListCommand);
SaveCommand = new RelayCommand(ExecuteSaveCommand);
NewAuthor = new Author();
}
private void ExecuteAddAuthorCommand()
{
db.Author.Add(_NewAuthor);
NewAuthor = new Author();
}
private void ExecuteDeleteAuthorCommand()
{
db.Author.Remove(SelectedAuthor);
}
private void ExecuteRefreshListCommand()
{
db.Author.Load();
Authors = db.Author.Local;
}
private void ExecuteSaveCommand()
{
db.SaveChanges();
}
private bool CanExecuteAddAuthorCommand()
{
return (!string.IsNullOrEmpty(NewAuthor.Name) && NewAuthor.Birthday != null);
}
private bool CanExecuteDeleteAuthorCommand()
{
return SelectedAuthor != null;
}
}
}
XAML:
<StackPanel>
<DataGrid ItemsSource="{Binding Authors}" SelectedItem="{Binding SelectedAuthor}" AutoGenerateColumns="False" Height="200" Margin="5" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Birthday" Binding="{Binding Birthday}"/>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name" Margin="5" VerticalAlignment="Center"/>
<TextBox Height="25" Width="70" Margin="5" Text="{Binding NewAuthor.Name}"/>
<TextBlock Text="Geburtstag" Margin="5" VerticalAlignment="Center"/>
<DatePicker Height="25" Width="130" Margin="5" SelectedDate="{Binding NewAuthor.Birthday}"/>
<Button Margin="5" Height="25" Width="80" Content="Hinzufügen" HorizontalAlignment="Left" Command="{Binding AddAuthorCommand}"/>
<Button Margin="5" Height="25" Width="80" Content="Löschen" HorizontalAlignment="Left" Command="{Binding DeleteAuthorCommand}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Width="120" Height="25" Content="Speichern" HorizontalAlignment="Left" Margin="5" Command="{Binding SaveCommand}"/>
<!--Button Width="120" Height="25" Content="Aktualisieren" HorizontalAlignment="Left" Margin="5" Command="{Binding RefreshCommand}"/-->
</StackPanel>
</StackPanel>
有什么想法為什么按鈕不使用canexecute函數?
您的問題是您使用的名稱空間:
using GalaSoft.MvvmLight.Command;
替換為:
using GalaSoft.MvvmLight.CommandWpf;
WPF的CommandManager在檢測到UI更改時調用CanExecute
? 什么是UI更改? 添加和刪除元素, 一些綁定更新,視覺狀態更改。 基本上,只要WPF有這種感覺。 這是不可靠的。
你怎么能穿裙子? 當您需要刷新RelayCommand
,請調用RelayCommand.RaiseCanExecuteChanged()
。 此方法引發一個事件,通知WPF它應重新評估命令的狀態。
看來您幾天前和我有同樣的問題。
ICommand.CanExecute
沒有調用,因為您當然不會說按鈕:“當我在TextBox
插入內容時,按鈕單擊的能力可能會改變”。 因此,如果文本框文本發生更改,您的按鈕不會意識到可以啟用他。 您必須明確告訴按鈕,當文本更改時,應該通過在文本框tex更改時引發ICommand.CanExecuteChanged
事件來檢查ICommand.CanExecute
。
我認為,不幸的是,您必須在分配SelectedAuthor並攔截NewAuthor對象的屬性更改后,調用RelayCommand對象的RaiseCanExecuteChanged方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.