c# - WPF DataTemplate 显示 ToString

标签 c# wpf mvvm

我正在开发一个小程序,并尝试使用 DataTemplate 遵循 MVVM 方法来为我的 VM 创建 View 。我遇到的问题是,它没有创建 View 的实例,而是显示了 VM 的 ToString() 结果。如果我在 View 类 (MainNavigation.xaml) 的构造函数上放置一个断点,它永远不会到达。

下面的代码,感谢帮助。

主窗口.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>

主窗口.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>

最佳答案

在 MainWindow.xaml 文件中根据 Sinatr 的评论更改了以下行:

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

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

关于c# - WPF DataTemplate 显示 ToString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46932406/

相关文章:

c# - 需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。在 HTMLAgilityPack 中

c# - Mef:尝试从另一个应用程序域加载ApiController时,“无法序列化”

c# - 如何在 WPF 项目中将 rdlc 文件添加到 ReportViewer

c# - 读取 XML 文件时出错

c# - 比较两个对象列表 C#

c# - 我想做的是 FreezableCollection.AddRange(collectionToAdd)

mvvm - 为什么 Xamarin 跨平台使用 MVVM

wpf - 来自源的背景图像数据绑定(bind)不起作用

c# - 如何禁用 Alt + 箭头关键帧导航?

c# - 使用C#获取当前位置