简体   繁体   中英

ContentControl to display UserControl crashes VS XAML UI Designer

I have a ContentControl with the Content property bound to a UserControl in my ViewModel, like I read in this question: WPF: how do I load user controls dynamically? However when trying to test this, my app doesn't even run, I get a message that the Visual Studion XAML UI Designer has crashed unexpectedly, leaving me without any clue how and why this is happening.

Also, in my code you may notice I use a UserControl instead of FrameworkElement as suggested in the above mentioned question, I tried both but they each give me the same error.

EDIT: After some testing, I found out the problem lies in this line of code, although I don't see why

    private UserControl _currentControl = new TableView();

EDIT2: There code above shouldn't be a problen though because TableView is a UserControl , and when I build the application I get no errors

As always, any help will be greatly appreciated!

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);
        }
    }
...

pls dont call your class MainViewModel and put code in it with UserControl! thats not the MVVM way :)

if you wanna handle some kind of CurrentWorkspace in MVVM then you should work with Viewmodels and DataTemplates. so instead to set a usercontrol to the CurrentWorkspace - simply set a viewmodel.

all the ContentControl Content property needs is a Datatemplate to know how to render your viewmodel.

MainViewModel.cs

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

xaml

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

content control stays the same, but needs a datatemplate to render your viewmodel

xaml - resources

 <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM