簡體   English   中英

用於顯示UserControl的ContentControl崩潰VS XAML UI Designer

[英]ContentControl to display UserControl crashes VS XAML UI Designer

我有一個ContentControl ,其Content屬性綁定到ViewModel中的UserControl ,就像我在這個問題中看到的: WPF:如何動態加載用戶控件? 但是,當嘗試對此進行測試時,我的應用程序甚至無法運行,我收到一條消息,提示Visual Studion 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);
        }
    }
...

請不要調用您的類MainViewModel並使用UserControl將代碼放入其中! 那不是MVVM方式:)

如果您想在MVVM中處理某種CurrentWorkspace,則應使用Viewmodels和DataTemplates。 因此,將用戶控件設置為CurrentWorkspace只需設置一個視圖模型即可。

ContentControl Content屬性所需的全部是一個Datatemplate,以了解如何呈現視圖模型。

MainViewModel.cs

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

a

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

內容控件保持不變,但需要一個數據模板來呈現您的視圖模型

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM