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

动画超过 2013-11-13 12:47:56

<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 NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}


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 DelegateCommand : ICommand
{
public bool CanExecute(object parameter)
{
if (CanExecuteFunc == null)
{
return true;
}
return CanExecuteFunc(parameter);
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
ExecuteAction(parameter);
}
public Action<object> ExecuteAction { get; set; }
public Func<object, bool> CanExecuteFunc { get; set; }
public DelegateCommand(Action<object> Execute, Func<object, bool> CanExecute)
{
CanExecuteChanged = (x, y) => { };
ExecuteAction = Execute;
CanExecuteFunc = CanExecute;
}
}


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


上面的示例,是一个以MVVM模式写的代码。Button的Command绑定RemoveCommand命令上,当在ListBox中选择项的时候,可以执行删除命令了,为什么Button还是灰色的呢?
...全文
415 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
动画超过 2013-11-14
  • 打赏
  • 举报
回复
引用 7 楼 MicrosoftCenterOfHN 的回复:
主要是CanExecuteChanged的定义。
果然是这个问题诶,我想请问下, public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } 这个是什么语法哦,匿名方法吗?value是什么意思?
动画超过 2013-11-13
  • 打赏
  • 举报
回复
引用 7 楼 MicrosoftCenterOfHN 的回复:
   
   
你的DelegateCommand的问题,试试上面的代码。 主要是CanExecuteChanged的定义。
果然是这个问题诶,我想请问下, public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } 这个是什么语法哦,匿名方法吗?value是什么意思?
小猪八Q 2013-11-13
  • 打赏
  • 举报
回复
DelegateCommand的第二个参数不是你想的那样,CanExecute是在window的InitializeComponent();的时候触发的,来判断是否可点击和执行,这个适合权限管理的那种,在CanExecute中加入权限判断的code,这是很方便的。 至于DelegateCommand以及内部更深层次的东西我不了解,而且在现有代码的基础上修改也无法完成,你可以使用另外一种方法,就是Converter

IsEnabled="{Binding Path=SelectedItem,ElementName=listbox,Converter={StaticResource xxx}}"
在Converter中判断,如果为null,则返回false,否则返回true 参考下吧,希望能帮到你
  • 打赏
  • 举报
回复
    class DelegateCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            if (CanExecuteFunc == null)
            {
                return true;
            }
            return CanExecuteFunc(parameter);
        }
        public void Execute(object parameter)
        {
            ExecuteAction(parameter);
        }
        public Action<object> ExecuteAction { get; set; }
        public Func<object, bool> CanExecuteFunc { get; set; }
        public DelegateCommand(Action<object> Execute, Func<object, bool> CanExecute)
        {
            ExecuteAction = Execute;
            CanExecuteFunc = CanExecute;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
你的DelegateCommand的问题,试试上面的代码。 主要是CanExecuteChanged的定义。
Andy__Huang 2013-11-13
  • 打赏
  • 举报
回复
private Person selectPerson; public Person SelectPerson 你这样定义可能自己都混淆,建议改为 private Person _SelectPerson; public Person SelectPerson 这样看起来明显一些
Andy__Huang 2013-11-13
  • 打赏
  • 举报
回复
.xaml.cs文件有没有这样?
//类名:OrderReportUploadLog

public partial class OrderReportUploadLog : ChildWindow
{
    public OrderReportUploadLog()
    {
        InitializeComponent();
    }

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
    }


}
还有:
bool RemoveCanExecute(object param)
{
    if (SelectPerson == null)
        return false;
    return true;
}
改为:
private bool RemoveCanExecute(object param)
{
    return true;
}

动画超过 2013-11-13
  • 打赏
  • 举报
回复
有人知道吗?
动画超过 2013-11-13
  • 打赏
  • 举报
回复
引用 2 楼 q107770540 的回复:
上帖你没给出class DelegateCommand : ICommand 的定义
嗯,这贴给出了
q107770540 2013-11-13
  • 打赏
  • 举报
回复
上帖你没给出class DelegateCommand : ICommand 的定义

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧