单击按钮时命令不执行

时间:2015-08-08 13:21:10

标签: wpf xaml

如何将集合绑定到Listview并让按钮执行命令以将单个用户添加到集合中。

查看型号代码:

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

查看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}"  />

1 个答案:

答案 0 :(得分:2)

错误在于按钮与命令的绑定:

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

应该是:

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

要查找此类错误,请始终阅读调试输出。在这种情况下,它很快显示:

  

System.Windows.Data错误:40:BindingExpression路径错误:   'object'''UserViewModel'

上找不到'UpdateCommad'属性

要防止这种情况发生,您可以通过设置设计时DataContext类型来使用Xaml代码完成:

<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}">

设置完成后,您将在Xaml中完成代码完成:只需开始键入绑定路径,它就会提供有效的选项。

Xaml code completion