我有ICommand接口的问题。我的按钮无法正常工作。我希望在填写name和id时启用它。似乎name和id没有值。至少我认为。我尝试过创建MainWindow的新对象并引用字段文本,但它没有用
XAML
<Window x:Class="WpfApplication1.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:m="clr-namespace:WpfApplication1"
xmlns:mv="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<m:Repository x:Key="P"/>
<mv:ViewM x:Key="C"/>
</Window.Resources>
<Border CornerRadius="23" Background="Black" Padding="10">
<StackPanel DataContext="{Binding Source={StaticResource C}}">
<TextBlock Foreground="White" HorizontalAlignment="Center" Text="Main" Padding="0,5,0,0"/>
<Separator Margin="0,10,0,0"/>
<TextBox HorizontalAlignment="Center" Height="30" Width="200" IsReadOnly="False" Text="{Binding ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Center" Padding="0,5,0,0" Margin="0,4,0,0" MaxLength="20"/>
<TextBox HorizontalAlignment="Center" Height="30" Width="200" IsReadOnly="False" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Center" Padding="0,5,0,0" Margin="0,4,0,0" MaxLength="20"/>
<TextBox HorizontalAlignment="Center" Height="30" Width="200" IsReadOnly="True" Text="{Binding Full, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Center" Padding="0,5,0,0" Margin="0,4,0,0" MaxLength="20"/>
<Button HorizontalAlignment="Center" Height="20" Width="100" Content="CHuj" Margin="0,20,0,0"
Command="{Binding com}" CommandParameter="{Binding Repo, UpdateSourceTrigger=PropertyChanged}"
</Button>
</StackPanel>
</Border>
ICommand的
public class Icom : ICommand
{
public ViewM VieM { get; set; }
public event EventHandler CanExecuteChanged;
public Icom(ViewM view)
{
this.VieM = view;
}
public bool CanExecute(object parameter)
{
Repository R = (Repository)parameter;
if (R != null)
{
if (string.IsNullOrEmpty(R.Name) || (string.IsNullOrEmpty(R.ID)))
return false;
return true;
}
return false;
}
public void Execute(object parameter)
{
VieM.Wy();
}
}
知识库
public class Repository : INotifyPropertyChanged
{
private string _id;
public string ID
{
get { return _id; }
set { _id = value;
OnPropertyChanged("ID");
OnPropertyChanged("Full");
}
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value;
OnPropertyChanged("Name");
OnPropertyChanged("Full");
}
}
private string _full;
public string Full
{
get { return Name + " " + ID; }
set { _full = value;
OnPropertyChanged("Full");
}
}
public Repository()
{
this.Name = "";
this.ID = "";
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
视图模型
public class ViewM : INotifyPropertyChanged
{
private string _id;
public string ID
{
get { return _id; }
set
{
_id = value;
Repo = new Repository()
{
Full = this.Full,
Name = this.Name,
ID = this.ID
};
OnPropertyChanged("ID");
OnPropertyChanged("Full");
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
Repo = new Repository()
{
Full = this.Full,
Name = this.Name,
ID = this.ID
};
OnPropertyChanged("Name");
OnPropertyChanged("Full");
}
}
private string _full;
public string Full
{
get { return Name + " " + ID; }
set
{
_full = value;
Repo = new Repository()
{
Full = this.Full,
Name = this.Name,
ID = this.ID
};
OnPropertyChanged("Full");
}
}
private Repository _repo;
public Repository Repo
{
get { return _repo; }
set
{
_repo = value;
OnPropertyChanged("Repo");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public Icom com { get; set; }
public ViewM()
{
this.com = new Icom(this);
}
public void Wy()
{
MessageBox.Show("Text");
}
}
您的按钮需要重新评估在您的值更改后是否可以执行它。
CommandManager
仅关注确定命令目标何时发生变化的某些条件,例如键盘焦点的变化。在CommandManager
没有充分确定导致命令无法执行的条件变化的情况下,可以调用CommandManager.InvalidateRequerySuggested Method强制CommandManager
提升RequerySuggested
事件。
我知道了
public class Icom : ICommand
{
public ViewM VieM { get; set; }
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void RaiseCanExecuteChanged()
{
CommandManager.InvalidateRequerySuggested();
}
public Icom(ViewM view)
{
this.VieM = view;
}
public bool CanExecute(object parameter)
{
Repository R = (Repository)parameter;
if (R != null)
{
if (string.IsNullOrEmpty(R.ID) || (string.IsNullOrEmpty(R.Name)))
return false;
return true;
}
return false;
}
public void Execute(object parameter)
{
VieM.Wy();
}
}
并在视图中
private Repository _repo;
public Repository Repo
{
get { return _repo; }
set
{
_repo = value;
OnPropertyChanged("Repo");
com.RaiseCanExecuteChanged();
}
}