您的位置:首页 > 其它

理解Prism中MVVM的Command与CommandParameter

2012-04-14 09:45 260 查看

内容摘要

接上一讲(/article/4661057.html),这一讲中我使用Prism做了演示和比较。Prism不仅仅是一个MVVM框架,它还包含其他的模块。在MVVM这个层面,Prism有些特殊性(Command绑定有特殊语法),这也是我这一讲的主要内容。

Prism的下载链接

http://compositewpf.codeplex.com/

视频地址

http://www.tudou.com/programs/view/72Ag1kQt1RA/

示例代码

<Window x:Class="WPFPrismMvvm.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
xmlns:local="clr-namespace:WPFPrismMvvm"
xmlns:cmd="clr-namespace:Microsoft.Practices.Prism.Commands;assembly=Microsoft.Practices.Prism"
Width="525">
<Window.DataContext>
<local:MainWindowViewModel UserName="chenxizhang"></local:MainWindowViewModel>
</Window.DataContext>
<Grid>

<StackPanel>

<TextBox Text="{Binding UserName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>

<Button Content="Show"
cmd:Click.Command="{Binding ShowCommand}"
cmd:Click.CommandParameter="{Binding UserName}"></Button>
</StackPanel>
</Grid>
</Window>


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Input;
using System.Windows;

namespace WPFPrismMvvm
{
public class MainWindowViewModel:NotificationObject
{

private string _UserName;
public string UserName
{
get { return _UserName; }
set
{
if (_UserName != value)
{
_UserName = value;
RaisePropertyChanged("UserName");
}
}
}

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

}
}
}
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: