创建类似于 Notepad++ 的应用程序 - 如何在不使用代码隐藏的情况下填充用户控件的数据

Creating an application similar to Notepad++ - how do I populate data of a user control wihout using code behind

我是一个 WPF 初学者,很难这么快地理解这么多新概念(我有这个项目的最后期限,这是作业),包括 MVVM 设计模式。

所以我需要创建一个类似于Notepad++的应用程序。

每个文件都应在新选项卡中打开。我想我可能会为此使用 UserControl。 (我看过了,感觉是个不错的选择)

问题是我不知道如何从文件加载数据。

这是我的代码: MainWindow.xaml

<Window x:Class="WpfApp1.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:WpfApp1.ViewModel"
        mc:Ignorable="d"
        Title="Notepad-- - Octavian Niculescu" Height="450" Width="800">
    <Window.DataContext>
        <local:MainCommands/>
    </Window.DataContext>
    <Grid>
        <DockPanel>
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="_File">
                    <MenuItem Header="_New" />
                    <MenuItem Header="_Open"
                              Command="{Binding Path=OpenFile}"/>
                    <MenuItem Header="_Save" />
                    <MenuItem Header="_Save as" />
                    <Separator />
                    <MenuItem Header="_Exit" />
                </MenuItem>
                <MenuItem Header="_Search">
                    <MenuItem Header="_Find" />
                    <MenuItem Header="_Replace" />
                    <MenuItem Header="_Replace all" />
                    <Separator />
                    <MenuItem Header="_Exit" />
                </MenuItem>
                <MenuItem Header="_Help">
                    <MenuItem Header="_About" />
                    <Separator />
                    <MenuItem Header="_Exit" />
                </MenuItem>
            </Menu>
            <Menu DockPanel.Dock="Top"/>
        </DockPanel>
    </Grid>
</Window>

FileTab.xaml

<UserControl x:Class="WpfApp1.view.FileTab"
             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:WpfApp1.view"
             mc:Ignorable="d" 
             d:DesignHeight="420" d:DesignWidth="800">
    <Grid>
        <TextBox AcceptsReturn="True" Name="FileContent"/>
        <TextBlock HorizontalAlignment="Left" Margin="29,11,0,0" Text="FileName.txt" TextWrapping="Wrap" VerticalAlignment="Top"/>
    </Grid>
</UserControl>

MainCommands.cs(我有按钮的 ICommands)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Input;
using WpfApp1.ViewModel;
using WpfApp1.auxiliar;

namespace WpfApp1.ViewModel
{
    class MainCommands : INotifyPropertyChanged
    {
        private ICommand _openFile;
        public ICommand OpenFile
        {
            get
            {
                if (_openFile == null)
                {
                    _openFile = new RelayCommand(Commands.OpenFileFunc);
                }
                return _openFile;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

和Commands.cs我有打开文件的功能

using System;
using System.Collections.Generic;
using System.Text;
using WpfApp1.view;

namespace WpfApp1.auxiliar
{
    static class Commands
    {
        public static void OpenFileFunc(object parameter)
        {
            Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();

            Nullable<bool> result = openFileDlg.ShowDialog();
if (result == true)
    {
        user control textblock = openFileDlg.FileName;
        user control textbox = System.IO.File.ReadAllText(openFileDlg.FileName);
    }
        }
    }
}

对话框打开。它工作正常。但是现在我必须将“用户控件文本块”和“用户控件文本框”绑定到数据,我不知道该怎么做。我不想(我也不被允许)使用隐藏代码。

如果重要的话,这是我的文件夹结构。

我的问题是如何使用来自 Commands.cs 的 if 块中的数据填充 FileTab 用户控件中的这两个字段?

谢谢。

您的 viewmodel 文件夹中应该有一些视图模型,例如 MainWindowViewModelFileTabViewModel。这些视图模型应该具有相应视图绑定到的属性。解决这个问题的一种方法是

FileTabVM:

namespace WpfApp1.ViewModel
{
    class FileTabViewModel : INotifyPropertyChanged
    {
        ...

        public string FileContent { ... }
        public string FileName { ... }

       ...
    }
}

MainWindowVM:

namespace WpfApp1.ViewModel
{
    class MainWindowViewModel : INotifyPropertyChanged
    {
        ...

        public ObservableCollection<FileTabViewModel> FileTabs { ... }

        ...
    }
}

然后将打开的命令逻辑放在 MainWindowVM 中,然后用它创建一个 FileTabVM 实例,其中包含它需要的数据。

Here's 一个很好的博客 post 如果您需要更详细的指南。