MVVM实践中的Command与CommandParameter的使用
生活随笔
收集整理的這篇文章主要介紹了
MVVM实践中的Command与CommandParameter的使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
內(nèi)容摘要
這一講,我在原先一篇博客文章(http://www.cnblogs.com/chenxizhang/archive/2011/10/01/2197786.html)基礎(chǔ)上,針對(duì)MVVM中Command的使用做了演示和講解。靈活的數(shù)據(jù)綁定,和命令綁定,是MVVM的核心精神,善加這兩個(gè)功能,將大大地簡(jiǎn)化我們的應(yīng)用程序開(kāi)發(fā),提供更加合理的代碼架構(gòu)。可以這么說(shuō),如果你在做WPF,Silverlight或者相關(guān)的開(kāi)發(fā),你是必須要了解MVVM的。但是至于你使用具體哪一個(gè)框架,倒不是那么重要的,他們基本都很類似。
?
視頻地址
http://www.tudou.com/programs/view/SZXSes10MD0/
?
示例代碼
using System.Windows; using System.Windows.Input; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command;namespace WpfMVVM {public class MainWindowViewModel:ViewModelBase{private string _UserName;public string UserName{get { return _UserName; }set{if (_UserName != value){_UserName = value;RaisePropertyChanged("UserName");}}}public ICommand ShowCommand{get{return new RelayCommand<string>((user) =>{MessageBox.Show(user);}, (user) => {return !string.IsNullOrEmpty(user);});}}} }?
?
<Window x:Class="WpfMVVM.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow"xmlns:local="clr-namespace:WpfMVVM"Height="350"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"Command="{Binding ShowCommand}"CommandParameter="{Binding UserName}"></Button></StackPanel></Grid> </Window>轉(zhuǎn)載于:https://www.cnblogs.com/chenxizhang/archive/2012/04/13/2446415.html
總結(jié)
以上是生活随笔為你收集整理的MVVM实践中的Command与CommandParameter的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 用 Microsoft Expressi
- 下一篇: 《JAVA与模式》之合成模式