1

I have three simple TextBoxes and two Buttons. In the TextBoxes are a name, a lastname and a fullname. if i press the first button (simple command) it should debug a "hello". This button is working well, but the second button (parameter command) is binded to fullname and should debug the person. But the button is disabled and i cant find the problem.

Person-class:

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));
    }
}

ParameterCommand-class:

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);
    }
}

Mainwindow in 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-class:

    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);
    }
}

The Problem is, that the CanExecute Methode in ParameterCommand will break at

if(parameter != null)

and return false. But as you can see, the lastname isn't null.

Morta
  • 224
  • 1
  • 14
  • Check out this (quite similar) question and the provided answer: https://stackoverflow.com/questions/30002300/how-to-use-the-canexecute-method-from-icommand-on-wpf – JonyVol Mar 22 '18 at 08:05
  • But i actually want to understand why it won't work and not just copy an other code.. – Morta Mar 22 '18 at 08:26

1 Answers1

1

Your event CanExecuteChanged lacks some necessary lines of code. It should look like this:

public event EventHandler CanExecuteChanged {
        add {
            CommandManager.RequerySuggested += value;
        }
        remove {
            CommandManager.RequerySuggested -= value;
        }
    }

Class CommandManager is responsible for raising the event if conditions in CanExecute method have changed. I suppose it should work now.

micnow21
  • 41
  • 1
  • 8