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