简体   繁体   English

WPF隐式数据模板不显示视图

[英]WPF implicit datatemplate not displaying view

fellow programmers. 同伴程序员。 I'm trying to build my first WPF app with tabbed interface. 我正在尝试使用选项卡式界面构建我的第一个WPF应用程序。 I've decided to not rely on any MVVM framework, since I'm only starting my journey. 我决定不依赖任何MVVM框架,因为我只是开始自己的旅程。

I've found quite a few tutorials, but can't make WPF to pick up correct view for viewmodels. 我已经找到了很多教程,但是不能使WPF为视图模型选择正确的视图。

What I have is Shell view, with underlying DataContext with ObservableCollection of my "Tabs viewmodels": 我所拥有的是Shell视图,以及带有“ Tabs视图模型”的ObservableCollection的基础DataContext:

    Shell.Xaml
    <Controls:MetroWindow x:Class="WpfApplication1.View.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Shell" Height="327" Width="667"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    xmlns:v="clr-namespace:WpfApplication1.View"
    xmlns:vm="clr-namespace:WpfApplication1.ViewModel"
    >
<Grid>
    <TabControl ItemsSource="{Binding Pages}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding DisplayName}" />
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.Resources>
            <DataTemplate DataType="x:Type vm:FirstPageModel">
                <v:FirstPageView />
            </DataTemplate>
            <DataTemplate DataType="x:Type vm:SecondPageModel">
                <v:SecondPageView />
            </DataTemplate>
        </TabControl.Resources>
    </TabControl>
</Grid>

All I with - to get my views inside tab's of tabcontrol. 我所拥有的-使我的视图进入tabcontrol的选项卡内。 But, no matter where I put my DataTemplate (window.resources, tabcontrol.resources, etc), I only get something like this: 但是,无论我将DataTemplate放在哪里(window.resources,tabcontrol.resources等),我都只会得到以下内容: 截图

As I can understand - somehow WPF doesn't see my views, but I don't understand why. 据我了解-WPF某种程度上看不到我的观点,但我不明白为什么。 My "views" are simple UserControls, like this: 我的“视图”是简单的UserControl,例如:

    <UserControl x:Class="WpfApplication1.View.FirstPageView"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBlock Text="{Binding Message}" Margin="36,131,47,127" />  
</Grid>

and corresponding ViewModel is: 相应的ViewModel是:

        public class FirstPageModel:BaseViewModel
            {
                public override string DisplayName
                {
                    get
                    {
                        return "First page";
                    }
                }
                public string Message { get { return "Hi from " + DisplayName; } }
            }

Could somebody tell me what's wrong with my code? 有人可以告诉我我的代码有什么问题吗?

Edit: 编辑:

ShellViewModel: ShellViewModel:

        public class ShellViewModel : BaseViewModel
{
    public override string DisplayName
    {
        get
        {
            return "First";
        }
    }
    private ObservableCollection<BaseViewModel> pages;
    public ObservableCollection<BaseViewModel> Pages
    {
        get{
            if(this.pages==null)
            {
                this.pages = new ObservableCollection<BaseViewModel>{
                    new FirstPageModel(),
                    new SecondPageModel()
                };
            }
            return this.pages;
        }
    }
}

App.xaml.cs: App.xaml.cs:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        Shell sh = new Shell();
        ShellViewModel shvm = new ShellViewModel();
        sh.DataContext = shvm;
        sh.Show();
    }
}

The DataTemplate definition is incorrect. DataTemplate定义不正确。 You must use curly braces for x:Type markup extension with DataType attribute value: 必须对带有DataType属性值的x:Type标记扩展名使用大括号:

<TabControl ItemsSource="{Binding Pages}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DisplayName}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.Resources>
        <!-- Use curly braces for x:Type markup extension -->
        <DataTemplate DataType="{x:Type vm:FirstPageModel}">
            <v:FirstPageView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type vm:SecondPageModel}">
            <v:SecondPageView />
        </DataTemplate>
    </TabControl.Resources>
</TabControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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