首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2013-11-14 
WPF,命令可以执行了,按钮为什么还是灰色的?Window.DataContextlocal:MainWindowViewModel//Window.Da

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 != null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}


class MainWindowViewModel:NotificationObject
{
    private  ObservableCollection<Person> people;
    public ObservableCollection<Person> People
    {
        get { return people; }
        set
        {
            people = value;
            this.OnPropertyChanged("People");
        }
    }
    private Person selectPerson;
    public Person SelectPerson
    {
        get { return selectPerson; }
        set
        {
            selectPerson = value;
            this.OnPropertyChanged("SelectPerson");
        }
    }
    public DelegateCommand RemoveCommand { get; set; }
    void RemoveExecute(object param) 
    {
        People.Remove(SelectPerson);


    }
    bool RemoveCanExecute(object param)
    {
        if (SelectPerson == null)
            return false;
        return true;
    }
    public MainWindowViewModel()
    {
        People = new ObservableCollection<Person>()
        {
            new Person(){Name="张三",Age=43},
            new Person(){Name="李四",Age=45},
            new Person(){Name="汤姆",Age=44},
        };
        RemoveCommand = new DelegateCommand(RemoveExecute,RemoveCanExecute);
    }
}


WPF,下令可以执行了,按钮为什么还是灰色的
上面的代码中,Button的Command绑定RemoveCommand命令上,当在ListBox中选择项的时候,命令可以执行了,为什么Button还是灰色的呢?
[解决办法]
没办法重现你的问题,提示:

'Microsoft.Practices.Composite.Presentation.Commands.DelegateCommand<T>'

已引用了 Microsoft.Practices.Composite.Presentation.dll

using Microsoft.Practices.Composite.Presentation.Commands;

你最好能提供你的app demo
[解决办法]
Error1Using the generic type 'Microsoft.Practices.Composite.Presentation.Commands.DelegateCommand<T>' requires 1 type arguments 
[解决办法]
将selectedItem这里修改下:
SelectedItem="{Binding SelectPerson,Mode=TwoWay}"
[解决办法]
引用:

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


            }
    }        
}



上面的代码中,Button的Command绑定RemoveCommand命令上,当在ListBox中选择项的时候,命令可以执行了,为什么Button还是灰色的呢?


button的IsEnable属性药想办法与Command的RemoveCanExecute关联起来

热点排行