我是一個 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>
檔案選項卡.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);
}
}
}
}
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.

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.
uj5u.com熱心網友回復:
您的 viewmodel 檔案夾中應該有一些視圖模型,例如 aMainWindowViewModel和 a FileTabViewModel。這些視圖模型應該具有各自視圖系結到的屬性。解決這個問題的一種方法是
FileTabVM:
namespace WpfApp1.ViewModel
{
class FileTabViewModel : INotifyPropertyChanged
{
...
public string FileContent { ... }
public string FileName { ... }
...
}
}
主視窗虛擬機:
namespace WpfApp1.ViewModel
{
class MainWindowViewModel : INotifyPropertyChanged
{
...
public ObservableCollection<FileTabViewModel> FileTabs { ... }
...
}
}
然后將您的打開命令邏輯放在 MainWindowVM 中,然后使用它創建 FileTabVM 的實體及其所需的資料。
如果您需要更詳細的指南,這里有一篇很好的博客文章。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439563.html
