2015-09-18 34 views
0

我需要根據模型中定義的值來切換視圖中創建視圖Catel和一個ContentPresenter和TemplateSelector

我的XAML是

<catel:DataWindow x:Class=" XX.Deals.Views.DealManageView" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
       xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
       xmlns:resources="clr-namespace:XX.Modules.Deals.Resources" 
       mc:Ignorable="d" 
       xmlns:catel="http://catel.codeplex.com" 
       xmlns:views="clr-namespace:XX.Modules.Deals.Views" 
       xmlns:selectors="clr-namespace:XX.Modules.Deals.Selectors" 
       xmlns:deals="clr-namespace:XX.Modules.Deals" 
       xmlns:viewmodels="clr-namespace:XX.Modules.Deals.ViewModels" 
       ResizeMode="CanResizeWithGrip"> 

<catel:DataWindow.Resources> 
    <DataTemplate DataType="{x:Type viewmodels:DealSpotUpdateViewModel}" x:Key="DealSpotUpdateView"> 
     <views:DealSpotUpdateView/> 
    </DataTemplate> 

    <DataTemplate DataType="{x:Type viewmodels:DealDepoUpdateViewModel}" x:Key="DealDepoUpdateView"> 
     <views:DealDepoUpdateView/> 
    </DataTemplate> 

    <DataTemplate DataType="{x:Type viewmodels:DealForwardUpdateViewModel}" x:Key="DealForwardUpdateView"> 
     <views:DealForwardUpdateView/> 
    </DataTemplate> 

    <DataTemplate DataType="{x:Type viewmodels:DealCurrencySwapUpdateViewModel}" x:Key="DealCurrencySwapUpdateView"> 
     <views:DealCurrencySwapUpdateView/> 
    </DataTemplate> 

    <selectors:DealUpdateTemplateSelector x:Key="DealUpdateTemplateSelector" 
              DealSpot="{StaticResource DealSpotUpdateView}" 
              DealDepo="{StaticResource DealDepoUpdateView}" 
              DealForward="{StaticResource DealForwardUpdateView}" 
              DealCurrencySwap="{StaticResource DealCurrencySwapUpdateView}" 
              /> 
</catel:DataWindow.Resources> 

<telerik:RadTabControl> 
    <telerik:RadTabItem Header="{x:Static resources:DealResources.LBL_DEAL_UPDATE}"> 
     <ContentPresenter Content="{Binding Deal}" ContentTemplateSelector="{ StaticResource DealUpdateTemplateSelector}"></ContentPresenter> 

    </telerik:RadTabItem> 
    <!--omiss--> 

和轉換器是這個

public class DealUpdateTemplateSelector : DataTemplateSelector 
{ 
    public DataTemplate DealDepo { get; set; } 
    public DataTemplate DealSpot { get; set; } 
    public DataTemplate DealForward { get; set; } 
    public DataTemplate DealCurrencySwap { get; set; } 

    public override DataTemplate SelectTemplate(object item, DependencyObject container) 
    { 
     if (item != null) 
     { 
      var deal = item as IDeal; 
      if (deal == null) return base.SelectTemplate(item, container); 

      switch (deal.Type) 
      { 
       case DealTypeHelper.DEPO: 
        return DealDepo; 
       case DealTypeHelper.CURRENCYSWAP: 
        return DealCurrencySwap; 
       case DealTypeHelper.FORWARD: 
        return DealForward; 
       case DealTypeHelper.SPOT: 
        return DealSpot; 
      } 
     } 

     return base.SelectTemplate(item, container); 
    } 
} 

的觀點被定義爲

public DealDepoUpdateView(DealDepoUpdateViewModel viewModel) 
     : base(viewModel, DataWindowMode.Custom, null, DataWindowDefaultButton.None, true, InfoBarMessageControlGenerationMode.None) 
    { 

當我嘗試加載我有一個例外認爲,視圖不能建立,因爲它沒有默認構造函數(我同意,因爲它有一個參數是視圖模型)

如何解決這件事?我看到在我身邊沒有解決辦法,因爲我沒有辦法設置視圖是如何創建的

您可以在這裏看到了異常:

Exception

感謝

回答

1

您需要的模型綁定在內容控制:

<ContentPresenter Content="{Binding Deal}" ContentTemplateSelector="{ StaticResource DealUpdateTemplateSelector}"></ContentPresenter> 

你的觀點需要一個空的構造函數:

public DealDepoUpdateView() 
{ 
} 

您的視圖模型需要接受模型:

public DealDepoUpdateViewModel(Deal deal /*, other services injected here */) 
{ 

}