Prism--MVVM 之Command

最近在做个项目,用到了MVVM模式。

发现在用DelegateCommand时,用到了CanExecute,不能实时更新,查了很多资料都没有这方面的。

经过仔细研究prism源码,发现以下解决方案:

下面是界面,很简单,一个textbox,一个button。实现的功能是

按下button时,显示textbox里的内容。

主要是当textbox内容为空时,button是不能使用的

 

<Grid>
        <TextBox  Margin="54,35,193,40" Name="textBox1"  />
        <Button Command="{Binding ShowMessage}" CommandParameter="{Binding Text, ElementName=textBox1}" Content="Button"  Margin="200,35,53,38"  />
    </Grid>
public ICommand ShowMessage
        {
            get
            {
                return new DelegateCommand<string>(
                    (str) =>
                    {
                        MessageBox.Show(str);
                    }, 
                    (str) =>
                    {
                        return !string.IsNullOrEmpty(str);
                    }
                );
            }
        }

运行起来,好像是没有什么问题,但当我们在textbox里填写内容时,发现button不会使能,这就有问题了。

也就是说,当textbox内容改变时,CanExecute不知道。

而怎么样才能知道呢,这里我们就要用到prism中的RaiseCanExecuteChanged这个方法了。

具体的代码以下:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="150" Width="373">
    <Grid>
        <TextBox Text="{Binding Message,UpdateSourceTrigger=PropertyChanged}" Margin="54,35,193,40" Name="textBox1"  />
        <Button Command="{Binding ShowMessage1}"  Content="Button"  Margin="200,35,53,38"  />
    </Grid>
</Window>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using System.Windows;

namespace WpfApplication1
{
    class mainWindowViewModel:NotificationObject
    {
        private string message;

        public string Message
        {
            get { return message; }
            set
            {
                message = value;
                RaisePropertyChanged("Message");
                ShowMessage1.RaiseCanExecuteChanged();
            }
        }


        public DelegateCommand ShowMessage1 {get;private set;}
       

        public ICommand ShowMessage
        {
            get
            {
                return new DelegateCommand<string>(
                    (str) =>
                    {
                        MessageBox.Show(str);
                    }, 
                    (str) =>
                    {
                        return !string.IsNullOrEmpty(str);
                    }
                );
            }
        }

        public mainWindowViewModel()
        {

            ShowMessage1 = new DelegateCommand(onExecute,onCanExecute);
            
        }

        private void onExecute()
        {
            MessageBox.Show(Message);
        }

        private bool onCanExecute()
        {
            return !string.IsNullOrEmpty(message);
        }
    }
}

 

 

 

 

posted @ 2014-05-16 22:42  枯无穷肉  阅读(948)  评论(0编辑  收藏  举报