【问题标题】:WPF implicit datatemplate not displaying viewWPF隐式数据模板不显示视图
【发布时间】:2015-06-02 04:35:34
【问题描述】:

各位程序员。 我正在尝试使用选项卡式界面构建我的第一个 WPF 应用程序。我决定不依赖任何 MVVM 框架,因为我才刚刚开始我的旅程。

我找到了很多教程,但无法让 WPF 为视图模型选择正确的视图。

我拥有的是 Shell 视图,其底层 DataContext 和我的“选项卡视图模型”的 ObservableCollection:

    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>

我所拥有的 - 让我的视图进入 tabcontrol 的选项卡。但是,无论我把我的 DataTemplate(window.resources、tabcontrol.resources 等)放在哪里,我都只会得到这样的东西:

据我所知 - WPF 不知何故看不到我的观点,但我不明白为什么。 我的“视图”是简单的用户控件,如下所示:

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

对应的 ViewModel 是:

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

谁能告诉我我的代码有什么问题?

编辑:

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:

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

【问题讨论】:

    标签: c# wpf xaml mvvm


    【解决方案1】:

    DataTemplate 定义不正确。您必须对带有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>
    

    【讨论】:

    • 我不敢相信我没有注意到这一点。谢谢。好吧,现在我遇到了另一个问题 - 视图没有从 ViewModel 获取值,出现错误,例如“因为数据项为空,所以找不到‘对象’的属性。这可能是因为数据提供者尚未生成任何数据”。你能帮我解决这个问题吗?
    • 请显示初始化 TabControl 并添加项目的代码。
    • 我已经添加了我的 ShellViewModel(显示我如何添加项目)和 App.xaml.cs(显示我如何启动我的应用程序)。我的 TabControl 完全驻留在 Shell.xaml 中,我没有在代码行为中初始化它(如果我理解你的问题的话)。
    • 好吧,在这里发表评论让有兴趣的人知道。我删除了我的外壳,然后重新创建了它。现在一切正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2021-09-17
    • 2020-05-12
    • 2018-09-23
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多