2012-08-24 197 views
0

這是我的ViewModel:DataTemplateSelector /綁定不起作用

class MainViewModel : ViewModelBase 
{ 
    private CalculatorViewModel calc; 
    public CalculatorViewModel Calculator {get {return calc; }set {calc = value; RaisePropertyChanged("Calculator");}} 
} 

class CalculatorViewModel : ViewModelBase 
{ 
    private CalculatorMode mode; 
    public CalculatorMode Mode {get {return mode;}set{mode=value; RaisePropertyChanged("CalculatorMode");}} 

    public CalculatorViewModel() 
    { 
     Mode = new HexMode(); 
    } 
} 
abstract class CalculatorMode : ViewModelBase 
{ 
    abstract int Calculate(...); 
} 
class HexMode : CalculatorMode 
{ 
public int Calculate(...) { ...} 
} 

這是我的主窗口(一個MainViewModel實例被設置爲Windows'的DataContext):

<ad:DockingManager > 
      <ad:DockingManager.LayoutItemTemplateSelector> 
       <vm:PanesTemplateSelector> 
        <vm:PanesTemplateSelector.FileViewTemplate> 
         <DataTemplate> 
          <h:MyDocument /> 
         </DataTemplate> 
        </vm:PanesTemplateSelector.FileViewTemplate> 
        <vm:PanesTemplateSelector.CalculatorViewTemplate> 
         <DataTemplate> 
          <w:Calculator /> 
         </DataTemplate> 
        </vm:PanesTemplateSelector.CalculatorViewTemplate> 
       </vm:PanesTemplateSelector> 
      </ad:DockingManager.LayoutItemTemplateSelector> 
</ad:DockingManager> 

的代碼PanesTemplateSelector級:

public DataTemplate FileViewTemplate 
     { 
      get; 
      set; 
     } 

     public DataTemplate CalculatorViewTemplate 
     { 
      get; 
      set; 
     } 

     public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) 
     { 
      if (item is DocumentViewModel) 
       return FileViewTemplate; 

      if (item is CalcualtorViewModel) 
       return CalculatorViewTemplate; 

      return base.SelectTemplate(item, container); 
     } 

計算器控制的內容:

<Grid Name="main"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <ContentControl Name="content"> 
     <ContentControl.ContentTemplateSelector> 
      <vm:CalculatorTemplateSelector> 
       <vm:CalculatorTemplateSelector.DefaultView> 
        <DataTemplate> 
         <TextBlock Text="Dummy for normal view" Foreground="Wheat" /> 
        </DataTemplate> 
       </vm:CalculatorTemplateSelector.DefaultView> 
       <vm:CalculatorTemplateSelector.HexView> 
        <DataTemplate> 
         <TextBlock Text="Dummy für Hexview" Foreground="Gray"/> 
        </DataTemplate> 
       </vm:CalculatorTemplateSelector.HexView> 
      </vm:CalculatorTemplateSelector> 
     </ContentControl.ContentTemplateSelector> 
    </ContentControl> 
</Grid> 

的CalculatorTemplateSelector級的代碼:

public DataTemplate HexView 
     { 
      get; 
      set; 
     } 

     public DataTemplate DefaultView 
     { 
      get; 
      set; 
     } 

     public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) 
     { 
      CalculatorViewModel cjmv = (CalculatorViewModel)item; 

      if (cjmv != null) 
      { 
       if (cjmv.Mode is HexMode) 
        return HexView; 

     return DefaultView; 
      } 

      return base.SelectTemplate(item, container); 
     } 

第一TemplateSelector正在工作。選擇了正確的模板,我可以看到計算器控制。但是Calculator-Control中的TemplateSelector不起作用。在CalculatorTemplateSelector.SelectTemplate中,項目參數始終爲空。但我需要那裏的CalculatorViewModel實例。我想我的問題是一個有約束力的問題,但我沒有找到解決方案。有任何想法嗎?

+1

謝爾頓·庫珀的通知..問一個問題!哦,我的日子!艾米如何? :P 無論如何,因爲你的問題可能是回答..你可能想要更改此行'CalculatorViewModel cjmv =(CalculatorViewModel)項目;'到'CalculatorViewModel cjmv =項目作爲CalculatorViewModel;'否則可能會拋出異常.. –

+0

+1謝爾頓庫珀問一個問題! – franssu

回答

0

TL;博士,但有一點是肯定的:

public CalculatorMode Mode [...] RaisePropertyChanged("CalculatorMode");}} 

應該是:RaisePropertyChanged("Mode");}}

0

我可以問,但只取你我輝煌的一部分...我只是找到了一個解決方案我: 添加DataContext的,綁定到一個ContentPresenter(而不是ContentControl中)

<ContentPresenter Name="content" DataContext="{Binding}"> 

,改變SelectTemplate的實現的:

public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) 
     { 
      ContentPresenter cp = container as ContentPresenter; 
      if (cp != null) 
      { 
       CalculatorViewModel model = cp.DataContext as CalculatorViewModel; 
       if (model != null) 
       { 
        if (model.Mode is DefaultMode) 
         return DefaultView; 
        if (model.Mode is HexMode) 
         return HexView; 
       } 
      } 
      return base.SelectTemplate(item, container); 
     } 

無論如何,感謝錯RaisePropertyChanged呼叫