SudhirWpf
SudhirWpf

Reputation: 141

Command not executing when clicking a button

How should the collection be bound to Listview and have the button execute the command to add a single user to the collection.

View Model Code:

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

public class UserViewModel : ViewModelBase
{
    private ObservableCollection<User> _UsersList;
    private User _user;
    public UserViewModel()
    {
        _user = new User();
        _UsersList = new ObservableCollection<User>();
        _UsersList.CollectionChanged += _UsersList_CollectionChanged;

    }

    private void _UsersList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {

    }

    public ObservableCollection<User> Users
    {
        get { return _UsersList; }
        set
        {
            _UsersList = value;
            OnPropertyChanged("Users");

        }
    }
    public User User
    {
        get
        {
            return _user;
        }
        set
        {
            _user = value;
            OnPropertyChanged("User");
        }
    }


    private ICommand mUpdater;
    public ICommand UpdateCommand
    {
        get
        {
            if (mUpdater == null)
                mUpdater = new Updater(this);
            return mUpdater;
        }
        //set
        //{
        //    mUpdater = value;
        //}
    }


    private void Submit()
    {

        Users.Add(User);
        User = new User();
    }

    private class Updater : ICommand
    {
        #region ICommand Members
        UserViewModel _obj;
        public Updater(UserViewModel obj)
        {
            _obj = obj;
        }
        public bool CanExecute(object parameter)
        {
            return true;
        }


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

        public void Execute(object parameter)
        {
            _obj.Submit();
        }
    }

View Xaml:

<ListView Name="UserGrid" 
          Grid.Row="1" 
          Margin="4,178,12,13"  
          ItemsSource="{Binding Users}"  >
    <ListView.View>
        <GridView x:Name="grdTest">
            <GridViewColumn Header="UserId" 
                            DisplayMemberBinding="{Binding UserId}"
                            Width="50"/>
        </GridView>
    </ListView.View>
</ListView>
<TextBlock Grid.Row="0" 
           Grid.Column="0" 
           Text="Name" 
           HorizontalAlignment="Center"/>
<TextBox Grid.Row="0" 
         Grid.Column="1" 
         Width="100" 
         HorizontalAlignment="Center"
         Text="{Binding User.UserId, Mode=TwoWay}"/>
<Button Content="Update" 
        Grid.Row="1" 
        Height="23" 
        HorizontalAlignment="Left" 
        Margin="310,40,0,0" 
        Name="btnUpdate" 
        VerticalAlignment="Top" 
        Width="141"
        Command="{Binding Path=UpdateCommad}"  />

Upvotes: 1

Views: 1618

Answers (1)

Emond
Emond

Reputation: 50672

The error is in the binding of the button to the command:

Command="{Binding Path=UpdateCommad}"  />

It should be:

Command="{Binding Path=UpdateCommand}"  />

To find errors like this always read the Debug Output. In this case it quickly showed:

System.Windows.Data Error: 40 : BindingExpression path error: 'UpdateCommad' property not found on 'object' ''UserViewModel'

To prevent this from happening at all you could use Xaml code completion by setting the design-time DataContext type:

<Window x:Class="TestWpfApplication.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:TestWpfApplication"
    mc:Ignorable="d"
    Title="MainWindow"
    Height="350"
    Width="525"
    d:DataContext="{d:DesignInstance Type=local:UserViewModel}">

After setting this you will have code-completion in Xaml: just start typing the binding path and it will offer valid options.

Xaml code completion

Upvotes: 2

Related Questions