码迷,mamicode.com
首页 > 其他好文 > 详细

Prism--MVVM 之Command

时间:2014-05-17 20:28:08      阅读:355      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

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

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

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

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

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

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

bubuko.com,布布扣

 

<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>
bubuko.com,布布扣
public ICommand ShowMessage
        {
            get
            {
                return new DelegateCommand<string>(
                    (str) =>
                    {
                        MessageBox.Show(str);
                    }, 
                    (str) =>
                    {
                        return !string.IsNullOrEmpty(str);
                    }
                );
            }
        }
bubuko.com,布布扣

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

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

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

具体的代码以下:

bubuko.com,布布扣
<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>
bubuko.com,布布扣

 

bubuko.com,布布扣
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);
        }
    }
}
bubuko.com,布布扣

 

 

 

 

Prism--MVVM 之Command,布布扣,bubuko.com

Prism--MVVM 之Command

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/amw2738/p/3733238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!