1
我正在开发一个WPF项目,通常我使用resourcedictionary来组织样式和颜色。当使用Expression Blend它会把视图模型对象在这样的资源:将视图模型放入资源辞典中是否正确?
<local:VM x:Key="VM" d:IsDataSource="True"/>
,并设置这样
<Window.DataContext><Binding Mode="OneWay" Source="{StaticResource VM}"/></Window.DataContext>
DataContext的,这是非常有用的使用source
得到命令或财产XAML在这样的结合(尤其是在DataTemplate中)
{Binding XXCommand,Source={StaticResource VM}}
财产我可以把视图模型对象ResourceDictionary中或者是最好把这个视图特定在与viewmodel相关的每个视图中?
而且,如果我把下面的风格在ResourceDictionary中我必须包括视图模型对象,
<Style x:Key="MenuItemStyle" TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Desc}"/>
<Setter Property="Icon" Value="{StaticResource IconImage}" />
<Setter Property="Command" Value="{Binding ChangeShowCommand,Source={StaticResource VM}}"/>
<Setter Property="CommandParameter" Value="{Binding}"/>
</Style>
它是一个不错的列表,但我强烈反对作者对他对'Option 7:DataTemplate as views'和'Option 8'的看法。这些是我将ViewModel绑定到View的最常用方法,而不是使用Option3来显示应用程序外壳的初始启动。 – Rachel 2012-01-30 17:59:56