【发布时间】:2018-03-22 07:15:14
【问题描述】:
我有三个简单的文本框和两个按钮。在文本框中是名称、姓氏和全名。如果我按下第一个按钮(简单命令),它应该调试一个“你好”。此按钮运行良好,但第二个按钮(参数命令)绑定到全名,应该调试人员。但是按钮被禁用,我找不到问题。
个人类:
class Person : INotifyPropertyChanged
{
public Person() {
Name = "Max";
Lastname = "Mustermann";
Fullname = Name + " " + Lastname;
}
private string name;
public string Name {
get { return name; }
set {
name = value;
OnPropertyChanged("Name");
OnPropertyChanged("Fullname");
}
}
private string lastname;
public string Lastname
{
get { return lastname; }
set
{
lastname = value;
OnPropertyChanged("Lastame");
OnPropertyChanged("Fullname");
}
}
private string fullname;
public string Fullname
{
get { return name +" "+ lastname; }
set
{
fullname = value;
OnPropertyChanged("Fullname");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
参数命令类:
public class ParameterCommand : ICommand
{
public ViewModelBase ViewModel { get; set; }
public ParameterCommand(ViewModelBase viewModel) {
this.ViewModel = viewModel;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
if(parameter != null) {
var s = parameter as String;
if (String.IsNullOrEmpty(s))
return false;
return true;
}
return false;
}
public void Execute(object parameter)
{
this.ViewModel.ParameterMethod(parameter as String);
}
}
XAML 中的主窗口:
<Window x:Class="MVVMPerson.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MVVMPerson"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="clr-namespace:MVVMPerson.Models"
xmlns:vm ="clr-namespace:MVVMPerson.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<m:Person x:Key="person"/>
<vm:ViewModelBase x:Key="viewModel"/>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource person}}">
<StackPanel VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBox Width="150" Text="{Binding Name, Mode=TwoWay}"/>
<TextBox Width="150" Text="{Binding Lastname, Mode=TwoWay}"/>
<TextBox Width="150" Text="{Binding Fullname}" SelectionOpacity="5"/>
<Button Content="simple command" Command="{Binding Path=SimpleCommand, Source={StaticResource viewModel}}"/>
<Button Content="parameter command" Command="{Binding ParameterCommand, Source={StaticResource viewModel}}"
CommandParameter="{Binding Fullname}" />
</StackPanel>
</Grid>
ViewModelBase 类:
public class ViewModelBase
{
public SimpleCommand SimpleCommand { get; set; }
public ParameterCommand ParameterCommand { get; set; }
public ViewModelBase() {
this.SimpleCommand = new SimpleCommand(this);
this.ParameterCommand = new ParameterCommand(this);
}
public void SimpleMethod() {
Debug.WriteLine("hello");
}
public void ParameterMethod(string person) {
Debug.WriteLine(person);
}
}
问题是,ParameterCommand 中的 CanExecute Methode 会中断
if(parameter != null)
并返回 false。但如您所见,姓氏不为空。
【问题讨论】:
-
查看这个(非常相似的)问题和提供的答案:stackoverflow.com/questions/30002300/…
-
但我实际上想了解为什么它不起作用,而不仅仅是复制其他代码..