2010-07-04 57 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