日期:2014-05-17  浏览次数:21295 次

WPF,命令可以执行了,按钮为什么还是灰色的?

<Window.DataContext>
    <local:MainWindowViewModel/>
</Window.DataContext>
<Grid HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="527">
    <ListBox ItemsSource="{Binding People}" SelectedItem="{Binding SelectPerson}" DisplayMemberPath="Name" 
                HorizontalAlignment="Left" Height="112" Margin="19,36,0,0" VerticalAlignment="Top" Width="123"/>
    <Button Command="{Binding RemoveCommand}" Content="删除" HorizontalAlignment="Left" Margin="171,129,0,0" 
            VerticalAlignment="Top" Width="75"/>
    <TextBox Text="{Binding SelectPerson.Name}" HorizontalAlignment="Left" Height="23" Margin="197,44,0,0" 
                TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox Text="{Binding SelectPerson.Age}" HorizontalAlignment="Left" Height="23" Margin="196,84,0,0" 
                TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBlock HorizontalAlignment="Left" Margin="161,50,0,0" TextWrapping="Wrap" Text="姓名" VerticalAlignment="Top"/>
    <TextBlock HorizontalAlignment="Left" Margin="161,88,0,0" TextWrapping="Wrap" Text="年龄" VerticalAlignment="Top"/>
</Grid>


class Person : NotificationObject 
{
    private string name;
    public string Name      
    {
        get { return name; }
        set {
            name = value;
            OnPropertyChanged("Name");
        }
    }
    private Nullable<int> age;
    public Nullable<int> Age  
    {
        get { return age; }
        set {
                age = value;
                OnPropertyChanged("Age");
            }
    }        
}


class NotificationObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged !=&