Home > Mobile >  Creating an application similar to Notepad - how do I populate data of a user control wihout using
Creating an application similar to Notepad - how do I populate data of a user control wihout using

Time:03-05

I'm a WPF beginner and it's kind of hard to wrap my head around so many new concepts so fast (I have a deadline for this project, it is homework), including the MVVM design pattern.

So I need to create an app similar to Notepad .

Each file should be opened in a new tab. I thought I might use an UserControl for that. (I've looked into it, and it seems a good choice)

The problem is that I don't know how to load the data from a file.

Here's my code: 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 (where I have the ICommands for the buttons)

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

and Commands.cs where I have the function for opening a file

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

The dialog opens. It works fine. But now I have to bind "user control textblock" and "user control textbox" to the data, and I don't know how to do it. I don't want (and I'm also not allowed) to use code behind.

Here's my folder structure, if it matters. photo

My question is how do I populate those two fields from the FileTab user control with the data from that if block from Commands.cs?

Thanks.

CodePudding user response:

You should have some view models in your viewmodel folder something like a MainWindowViewModel and a FileTabViewModel. Those view models should have properties that the respective views bind to. One way to approach this would be

FileTabVM:

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

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

       ...
    }
}

MainWindowVM:

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

        public ObservableCollection<FileTabViewModel> FileTabs { ... }

        ...
    }
}

Then put your open command logic in the MainWindowVM, then use that to create an instance of a FileTabVM with the data it needs.

Here's a good blog post if you need a more detailed guide.

  • Related