2010-07-04 54 views
6

Silverlight 4不存在了,看起来我们已经错过了此版本中的DataTemplate DataType功能,这对MVVM支持恕我直言很关键。对于我的WPF应用程序,在这一点上,我非常习惯于将我的视图的DataTemplates全局添加到我的Application.Resources中,并使用DataTypes为我的对应ViewModels创建了:Silverlight 4 DataTemplate DataType

ie。

<DataTemplate DataType="{x:Type viewModels:myViewModel}"> 
<views:myView/> 
</DataTemplate> 

我喜欢这种方式,因为我的所有绑定的ViewModels自动显示正确的内容......特别有用,当我有一些的ItemSource绑定到的ViewModels的集合,我的看法......这,例如,将自动确保绑定到Collection<SomeViewModel>的TabControl中的每个选项卡都显示与SomeViewModel关联的视图。

有些东西我试图为SL 3包括:

  • 创建“DataTemplatePresenterContentControl”,其自动应用一个DataTemplate用于当控制加载

  • 使用的TypeConverter内容,动态地施加控制负载,在可视树上行走以寻找数据绑定对象

  • 使用动态应用于控制负载的样式,沿着可视化树的外观绑定克数据对象

然而,这些方法都没有真正解决我以可接受的方式,这是非常关键上述情况。因此,由于这仍然不可能在Silverlight 4中开箱即用,所以我很想知道是否有人还没有提出一些合理的选择。

谢谢。

回答

8

我这样做是在几个商业项目的方式如下:

我有一个标准的IValueConverter

public class ViewTemplateChooser : IValueConverter 
{ 
    /// <summary> 
    /// Modifies the source data before passing it to the target for display in the UI. 
    /// </summary> 
    /// <returns> 
    /// The value to be passed to the target dependency property. 
    /// </returns> 
    /// <param name="value">The source data being passed to the target.</param><param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param><param name="parameter">An optional parameter to be used in the converter logic.</param><param name="culture">The culture of the conversion.</param> 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value is MyViewModel) 
     { 
      return new MyView { DataContext = value }; 
     } 

     return value; 
    } 

    /// <summary> 
    /// Modifies the target data before passing it to the source object. This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings. 
    /// </summary> 
    /// <returns> 
    /// The value to be passed to the source object. 
    /// </returns> 
    /// <param name="value">The target data being passed to the source.</param><param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param><param name="parameter">An optional parameter to be used in the converter logic.</param><param name="culture">The culture of the conversion.</param> 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

转换器需要一个命名空间注册

xmlns:Converters="clr-namespace:YourProject.Converters" 

然后您在资源部分参考了转换器:

<UserControl.Resources> 
    <Converters:ViewTemplateChooser x:Key="TemplateChooser" /> 
</UserControl.Resources> 

,最后我用转换器将视图模型转换为视图设置为视图模型

<ContentControl Content="{Binding Workspace, Converter={StaticResource TemplateChooser}}" Margin="5,35,5,5" Grid.Column="1" /> 

该转换器可进行修改,以实现导航的战略视图的DataContext的,我试图让这个例子尽可能简单。

我希望这会有所帮助,您不必去极端或第三方图书馆得到您要找的东西。

1

在WPF和Silverlight中,我使用Prism来做到这一点。我发现它根据类型转换视图更加灵活。它需要一点点才能把它束缚住,但一旦进入,可能性是无止境的。

编辑

我由RegionName绑定到我的视图模型属性(可能是的GetType()。如果你想名)做到这一点。然后,我注册名称的类型,它只是起作用。

在像一个ListBox的情况下,我成立了数据模板是:

<ContentControl Regions:RegionManager.RegionName="{Binding SomeName}" /> 

如果你不想SomeName是你要绑定到对象上,考虑ValueConverter那返回类型名称:

<ContentControl Regions:RegionManager.RegionName="{Binding SomeName, Converter={StaticResource ObjectToTypeConverter}}" /> 

这有帮助吗?

+0

我也在使用Prism的RegionManager,但是能否详细说明一下你如何做这件事的具体细节? – Jeff 2010-07-06 15:44:39

+0

详细说明在我上面的编辑中。 – 2010-07-06 17:03:35

+0

是的,谢谢。我喜欢这种方法。但它仍不能解决我上面提到的一个问题;绑定到IEnumerable - 例如将TabControl绑定到集合并期望每个选项卡显示MyViewForMyViewModelClass UserControl。或者有什么办法来调整你的方法来支持它?谢谢。 – Jeff 2010-07-06 21:37:40

相关问题