2013-05-31 42 views
0

我與Content財產在我的ViewModel綁定到UserControl,就像我在這個問題讀ContentControlWPF: how do I load user controls dynamically?但是試圖測試這個時候,我的應用程序不會運行,我收到一條消息,即Visual Studion XAML UI Designer意外崩潰,讓我毫無頭緒地知道如何以及爲何發生這種情況。ContentControl中顯示用戶控件崩潰VS XAML UI設計師

此外,在我的代碼中,您可能會注意到我使用UserControl而不是FrameworkElement,正如上面提到的問題中所建議的,我嘗試了兩種方法,但他們每個都給我出現了相同的錯誤。

編輯:經過一些測試,我發現了問題就出在這行代碼,但我不明白爲什麼

private UserControl _currentControl = new TableView(); 

EDIT2:有以上代碼應該沒有一點改進,但​​因爲TableView中是UserControl,當我構建應用程序,我得到沒有錯誤

一如既往,任何幫助將不勝感激!

MainWindow.xaml

<Fluent:RibbonWindow x:Class="DatabaseExplorer.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:Fluent="clr-namespace:Fluent;assembly=Fluent" 
     xmlns:View="clr-namespace:DatabaseExplorer.Views" 
     xmlns:ignore="http://www.ignore.com" 
     mc:Ignorable="d ignore" 
     Height="300" 
     Width="300" 
     Title="Application" 
     DataContext="{Binding Main, Source={StaticResource Locator}}"> 

    <Fluent:RibbonWindow.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Skins/MainSkin.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Fluent:RibbonWindow.Resources> 

    <Grid x:Name="LayoutRoot"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <Fluent:Ribbon> 
      ... 
     </Fluent:Ribbon> 
     <ContentControl Grid.Row="1" Content="{Binding CurrentControl}" /> 
    </Grid> 
</Fluent:RibbonWindow> 

MainViewModel.cs

... 
    /// <summary> 
    /// The <see cref="CurrentControl" /> property's name. 
    /// </summary> 
    public const string CurrentControlPropertyName = "CurrentControl"; 

    private UserControl _currentControl = new TableView(); 

    /// <summary> 
    /// Sets and gets the CurrentControl property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 
    public UserControl CurrentControl 
    { 
     get 
     { 
      return _currentControl; 
     } 

     set 
     { 
      if (_currentControl == value) 
      { 
       return; 
      } 

      RaisePropertyChanging(CurrentControlPropertyName); 
      _currentControl = value; 
      RaisePropertyChanged(CurrentControlPropertyName); 
     } 
    } 
... 
+0

最PROBA在XAML中出現一些bug。確保你的XAML是有效的 –

+0

@HarisHasan沒有不完全的,刪除我的視圖中的'Content'綁定和我的ViewModel中的'UserControl'屬性解決了這個問題。 – Kryptoxx

回答

1

請不要打電話給你的類MainViewModel並把代碼它用戶控件!那不是MVVM的方式:)

如果你想在MVVM中處理某種CurrentWorkspace,那麼你應該使用Viewmodels和DataTemplates。所以改爲將用戶控件設置爲CurrentWorkspace - 只需設置視圖模型即可。

所有的ContentControl Content屬性需要的是一個Datatemplate來了解如何渲染你的viewmodel。

MainViewModel.cs

public object CurrentWorkspace {get;set;} 
... 
this.CurrentWorkspace = this._myViewmodelInstanceWithSomeCoolThings; 

XAML

<ContentControl Grid.Row="1" Content="{Binding CurrentWorkspace }" /> 

內容控制保持不變,但需要一個DataTemplate來渲染視圖模型

XAML - 資源

<Fluent:RibbonWindow.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Skins/MainSkin.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
     <DataTemplate DataType="{x:Type local:ViewModelWithSomeCoolthingsClass}"> 
      <local:MyViewForViewModelWithSomeCoolthingsClass /> 
     </DataTemplate> 
    </ResourceDictionary>  
</Fluent:RibbonWindow.Resources> 
+0

非常感謝你,現在完美!我是整個MVVM場景的新手,所以請原諒我這樣的基本錯誤。我現在唯一的問題是,我不能在Window.Resources內容中放置超過1個元素,所以我失去了我的頁面樣式。我最好怎麼處理這個? – Kryptoxx

+0

沒問題。您當然可以將數據模板放在您的資源詞典中(參見我的編輯),或者您可以爲所有數據模板創建一個新的Resourcedictionarys並將其添加到合併字典 – blindmeis

+0

太棒了,完美無缺,是啊,也許我應該爲他們製作資源詞典,似乎是適當的;)另外一個問題,我可能完全錯誤,但由於我的所有視圖都是在ViewModelLocator類中聲明的,是否可以在那裏檢索它們,而不是爲MainViewModel中的每個視圖創建一個新實例? – Kryptoxx