Silverlight MVVM
MVVM是Model-View-ViewModel的简写。
View绑定到ViewModel,然后执行一些命令在向它请求一个动作。而反过来,ViewModel跟Model通讯,告诉它更新来响应UI。这样便使得为应用构建UI非常的容易。往一个应用程序上贴一个界面越容易,外观设计师s就越容易使用Blend来创建一个漂亮的界面。同时,当UI和功能越来越松耦合的时候,功能的可测试性就越来越强。
下面来看一个简单的实例
MainPage.xaml 文件
<Grid x:Name="LayoutRoot" Background="White">
<my:BookManageControl HorizontalAlignment="Stretch" x:Name="bookManageControl1" VerticalAlignment="Stretch" />
</Grid>
BookManageControl.xaml
View Code
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding Path=BookInfos}" x:Name="lst">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"></TextBlock>
<TextBlock Text="{Binding Count}"></TextBlock>
<TextBlock Text="{Binding PublishDate}"></TextBlock>
<Button Content="删除" Width="60" Height="30" Command="{Binding Path=DataContext.DeleteCommad, ElementName=root}" CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource TemplatedParent}}"></Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Column="1">
<StackPanel DataContext="{Binding Path=SelectedItem, ElementName=lst}">
<TextBlock Text="书名"></TextBlock>
<TextBox Text="{Binding Name}"></TextBox>
<TextBlock Text="数量"></TextBlock>
<TextBox Text="{Binding Count}"></TextBox>
<TextBlock Text="出版日期"></TextBlock>
<sdk:DatePicker SelectedDate="{Binding PublishDate}"/>
</StackPanel>
</Grid>
</Grid>
public partial class BookManageControl : UserControl
{
public BookManageControl()
{
InitializeComponent();
this.DataContext = new BookManageViewModel();
}
}
BookManageViewModel.cs
View Code
public class BookManageViewModel : ViewModel
{
private List<BookInfo> _bookInfos;
private ICommand _deleteCommad;
public List<BookInfo> BookInfos
{
get
{
if (this._bookInfos == null)
{
this._bookInfos = new List<BookInfo>();
this._bookInfos.Add(new BookInfo()
{
Name = "WPF 4.0 揭秘",
Count = 5400,
PublishDate = new DateTime(2010, 7, 21)
});
this._bookInfos.Add(new BookInfo()
{
Name = "大话设计模式",
Count = 3000,
PublishDate = new DateTime(2009, 6, 12)
});
this._bookInfos.Add(new BookInfo()
{
Name = "征服 Ajax",
Count = 4000,
PublishDate = new DateTime(2007, 3, 9)
});
}
return this._bookInfos;
}
set
{
this._bookInfos = value;
this.RaisePropertyChanged("BookInfos");
}
}
public ICommand DeleteCommad
{
get
{
if (this._deleteCommad == null)
{
this._deleteCommad = new DeleteCommad();
}
return this._deleteCommad;
}
}
}
ViewModel.cs
public class ViewModel : IViewModel
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
IViewModel.cs
public interface IViewModel : System.ComponentModel.INotifyPropertyChanged
{
}
DeleteCommad.cs
public class DeleteCommad : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (parameter == null)
{
MessageBox.Show("NULL");
}
else
{
BookInfo book = parameter as BookInfo;
MessageBox.Show(book.Name);
}
}
}


浙公网安备 33010602011771号