【问题标题】:New UserControl not showing up新的用户控件未显示
【发布时间】:2017-03-24 10:23:35
【问题描述】:

我有UserControl 称为“LinqView”,其中有带按钮的菜单。

用户点击按钮后,我想使用 MVVM 显示新的UserControl

我创建了名为“UsersModel”的新模型类和名为“ViewUsersUserControl”的新UserControl

但我不知道为什么不起作用。

下面是我的 xaml 和 cs 代码。

LinqView.xaml

<UserControl x:Class="LayoutMVVM.Views.LinqView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:LayoutMVVM.Views"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"

             xmlns:veiwmodels="clr-namespace:LayoutMVVM.ViewModels.MojeDBModel"
             xmlns:views="clr-namespace:LayoutMVVM.Views.MojeDBViews" >             

    <UserControl.Resources>
        <DataTemplate x:Name="UsersTemp" DataType="{x:Type veiwmodels:UsersModel}">
            <views:ViewUsersUserControl DataContext="{Binding}" />
        </DataTemplate>
    </UserControl.Resources>

    <Grid Background="LemonChiffon">

        <Menu Height="32" Name="Menu" VerticalAlignment="Top">
            <MenuItem Header="_Menu">
                <MenuItem Header="Add User" Click="MenuItem_VU" />
            </MenuItem>
        </Menu>

    </Grid>
</UserControl>

LinqView.cs

private void MenuItem_VU(object sender, RoutedEventArgs e)
{
    DataContext = new UsersModel();
}

【问题讨论】:

    标签: c# wpf xaml mvvm user-controls


    【解决方案1】:

    这样试过了吗?

     <UserControl x:Class="LayoutMVVM.Views.LinqView"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:local="clr-namespace:LayoutMVVM.Views"
                     mc:Ignorable="d" 
                     d:DesignHeight="300" d:DesignWidth="300"
    
                     xmlns:veiwmodels="clr-namespace:LayoutMVVM.ViewModels.MojeDBModel"
                     xmlns:views="clr-namespace:LayoutMVVM.Views.MojeDBViews" >             
    
            <UserControl.Resources>
                <DataTemplate DataType="{x:Type veiwmodels:UsersModel}">
                    <views:ViewUsersUserControl DataContext="{Binding}" />
                </DataTemplate>
            </UserControl.Resources>
    
            <Grid Background="LemonChiffon">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Menu " Height="32" Name="Menu" VerticalAlignment="Top">
                    <MenuItem Header="_Menu">
                        <MenuItem Header="Add User" Click="MenuItem_VU" />
                    </MenuItem>
                </Menu>
               <ContentControl Grid.Row="1" Content="{Binding UsersModel}"/>
            </Grid>
        </UserControl>
    

    你的 C# 类应该是这样的,

    public class MainViewModel
    {
     public UsersModel UsersModel {get;set;}
     // other properties
    }
    

    然后在菜单中点击,

       private void MenuItem_VU(object sender, RoutedEventArgs e)
        {
            DataContext = new MainViewModel();
        }
    

    我已删除 DataTemplate 上的密钥。

    更新: 简单的工作示例,

    MainWindow.cs

    namespace WpfApplication29
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new MainViewModel();
            }
        }
    
        public class MainViewModel : INotifyPropertyChanged
        {
            public LinqViewModel LinqModel
            {
                get; set;
            } = new LinqViewModel();
    
            public MainViewModel()
            {
                SelectedMainModel = LinqModel;
            }
            private object selectedModel;
            public object SelectedMainModel
            {
                get
                {
                    return selectedModel;
                }
                set
                {
                    selectedModel = value;
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
            private void Notify(string name)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    
        public class LinqViewModel : INotifyPropertyChanged
        {
            public UserModel UserModel
            {
                get; set;
            } = new UserModel();
    
            private object selectedChildModel;
            public object SelectedChildModel
            {
                get
                {
                    return selectedChildModel;
                }
                set
                {
                    selectedChildModel = value;
                    Notify("SelectedChildModel");
                }
    
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void Notify(string name)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    
        public class UserModel
        {
    
        }
    }
    

    MainWindow.xaml

    <Window
        x:Class="WpfApplication29.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:local="clr-namespace:WpfApplication29"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="525"
        Height="350"
        mc:Ignorable="d">
        <Window.Resources>
            <DataTemplate DataType="{x:Type local:LinqViewModel}">
                <local:LinqView />
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:UserModel}">
                <local:UserView />
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <ContentControl Content="{Binding SelectedMainModel}" />
        </Grid>
    </Window>
    

    用户用户控件

    <UserControl
        x:Class="WpfApplication29.UserView"
        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:local="clr-namespace:WpfApplication29"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        d:DesignHeight="300"
        d:DesignWidth="300"
        mc:Ignorable="d">
        <Grid >
            <TextBlock Text="From UserView"/>
        </Grid>
    </UserControl>
    

    LinqView 用户控件 xaml

    <UserControl
        x:Class="WpfApplication29.LinqView"
        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:local="clr-namespace:WpfApplication29"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        d:DesignHeight="300"
        d:DesignWidth="300"
        mc:Ignorable="d">
        <Grid Background="LemonChiffon">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Menu  Height="32" Name="Menu" VerticalAlignment="Top">
                <MenuItem Header="_Menu">
                    <MenuItem Header="Add User" Click="MenuItem_VU" />
                </MenuItem>
            </Menu>
            <ContentControl Grid.Row="1" Content="{Binding SelectedChildModel}"/>
        </Grid>
    </UserControl>
    

    Linqview 用户控件 cs

    namespace WpfApplication29
    {
        /// <summary>
        /// Interaction logic for LinqView.xaml
        /// </summary>
        public partial class LinqView : UserControl
        {
            public LinqView()
            {
                InitializeComponent();
            }
    
            private void MenuItem_VU(object sender, RoutedEventArgs e)
            {
                (this.DataContext as LinqViewModel).SelectedChildModel = (this.DataContext as LinqViewModel).UserModel;
            }
        }
    }
    

    此示例遵循多级层次结构。希望这会有所帮助。

    【讨论】:

    • 我收到错误:PresentationCore.dll 中出现“System.InvalidOperationException”类型的未处理异常附加信息:布局递归达到允许的限制以避免堆栈溢出:“2047”。树包含循环或太深。
    • 请查看更新后的代码,因为两者共享相同的 DataContext,所以发生了问题。您可以尝试更新。
    • 可以上传项目示例吗?因为我仍然遇到同样的错误......因为进入 MenuItem_VU 我正在创建新的 UsersModel () 而不是 MainViewModel
    • 您需要为内部控件创建新模型,如果您对两个控件都使用相同的模型,它将无法正常工作。只需创建一个将用户模型作为属性的类.. 如果这不起作用,请告诉我
    • 打开第一个 UserControl (LinqView.xaml) 我正在使用名为 LinqViewModel 的模型。我有第二个模型 UsersModel.cs 和第二个 UserControl ViewUsersUserControl.xaml ........所以,在 LinqView.cs 我把你的代码放到按钮 (DataContext = new UsersModel()) ......哪里放 get并设置?
    【解决方案2】:

    如果您只有一个UsersModel,则不需要将其作为资源模板:

    <UserControl x:Class="LayoutMVVM.Views.LinqView"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:LayoutMVVM.Views"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300"
                 xmlns:veiwmodels="clr-namespace:LayoutMVVM.ViewModels.MojeDBModel"
                 xmlns:views="clr-namespace:LayoutMVVM.Views.MojeDBViews" >             
        <DockPanel Background="LemonChiffon">
            <Menu Height="32" Name="Menu" DockPanel.Dock="Top">
                <MenuItem Header="_Menu">
                    <MenuItem Header="Add User" />
                </MenuItem>
            </Menu>
            <views:ViewUsersUserControl />
        </DockPanel>
    </UserControl>
    

    【讨论】:

    • views:ViewUsersUserControl 应该在您单击按钮后打开.....现在我认为它在我的 LinqView.xaml 上(当我运行您的代码时)
    • 您的命名约定令人困惑...所以,您有UsersModel,其中包含UsersUserModel 的集合? (如果是这样,你的 DataTemplate 上有错误的 DataType + 我没有看到任何 ItemsControl
    • 在 MainWindow 中,我运行了一个名为 LinqViewModel 的模型,它打开了名为“LinqView”的 UserControl。现在进入这个我有我的按钮菜单(上面的代码),我想在其中打开名为“ViewUsersUserControl”的新用户控件
    • 试试WPFUsers 回答,我认为他成功了。
    猜你喜欢
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多