WPF数据模板显示ToString

3
我正在开发一个小程序,尝试使用DataTemplate遵循MVVM模式为我的VM创建视图。我遇到的问题是,它似乎显示了VM的ToString()结果,而不是创建视图实例。如果我在View类(MainNavigation.xaml)的构造函数上设置断点,则永远不会触发。
以下是代码,请帮忙查看。
<Window x:Class="Tester.Wpf.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:local="clr-namespace:Tester.Wpf"
    xmlns:scr="clr-namespace:Tester.Wpf.Screens"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <DataTemplate DataType="x:Type scr:MainNavigationViewModel">
        <scr:MainNavigation />
    </DataTemplate>
</Window.Resources>

<Grid>
    <ContentControl Name="MainNav" Content="{Binding MainNavigationViewModel2}" />
</Grid>
</Window>

MainWindow.xaml.cs

using System.ComponentModel;
using System.Windows;
using Tester.Wpf.Screens;

namespace Tester.Wpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private MainNavigationViewModel _mainNavigationViewModel;

        public MainWindow()
        {
            this.DataContext = this;
            this.MainNavigationViewModel2 = new MainNavigationViewModel();

            InitializeComponent();
        }

        public MainNavigationViewModel MainNavigationViewModel2
        {
            get { return _mainNavigationViewModel; }
            set { _mainNavigationViewModel = value;
                RaisePropertyChangedEvent("MainNavigationViewModel2");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChangedEvent(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

MainNavigation.xaml.cs

using System.Windows.Controls;

namespace Tester.Wpf.Screens
{
    /// <summary>
    /// Interaction logic for MainNavigation.xaml
    /// </summary>
    public partial class MainNavigation : UserControl
    {
        public MainNavigation()
        {
            InitializeComponent();
        }
    }
}

MainNavigationViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tester.Wpf.Helpers;

namespace Tester.Wpf.Screens
{
    public class MainNavigationViewModel : BaseViewModel
    {
        public int Test = 10;
    }
}

MainNavigation.xaml

<UserControl x:Class="Tester.Wpf.Screens.MainNavigation"
             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:Tester.Wpf.Screens"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <Grid Background="Blue">
         <TextBlock>YOU"VE MADE IT!</TextBlock>
    </Grid>
</UserControl>

MainNavigationViewModel2是什么?没有这样的属性,你也没有在任何地方设置DataContext。检查输出窗口中的绑定错误,它们应该很容易理解。我建议你找到MVVM教程并跟随它(例如这个)。 - Sinatr
抱歉,我已经添加了代码。我会查看教程,谢谢。我已经检查了很多(似乎有很多),但出于某种原因无法使其工作,可能是一些愚蠢而微小的问题。 - Will E.
感谢您的建议。虽然它没有解决主要问题,但确实减少了一些代码量。 - Will E.
4
难以察觉,但您误用了x:Type XAML扩展。请将其放在 { } 中。 - Sinatr
哈哈,我在前五秒就发现了,但那只是我的个人能力 :) - user1228
显示剩余2条评论
1个回答

5
在MainWindow.xaml文件中,根据Sinatr的评论更改了以下行:
<DataTemplate DataType="x:Type scr:MainNavigationViewModel">

为了

<DataTemplate DataType="{x:Type scr:MainNavigationViewModel}">

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接