WPF:ICommand,在文本框中打开一个 txt 文件
WPF: ICommand, open a txt file in textbox
我这里有一个小问题,我必须在基本的 WPF 记事本之类的东西中打开(读取)一个文本文件,但我必须处理 ICommand 界面。问题是,当我选择要打开的 txt 文件时,什么也没有发生,我只看到一个空的记事本。有什么解决办法吗?这是代码:
public class OpenCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
OpenFileDialog op = new OpenFileDialog();
op.Filter = "textfile|*.txt";
op.DefaultExt = "txt";
if(op.ShowDialog() == true)
{
File.ReadAllText(op.FileName);
}
}
}
也许 bindig 不是我现在真的不知道。
<MenuItem Header="File" >
<MenuItem Header="New"/>
<MenuItem Header="Open..." Command="{Binding MyOpenCommand}" CommandParameter="{Binding ElementName=textbox2, Path=Text}"/>
<MenuItem Header="Save..." Command="{Binding MySaveCommand}" CommandParameter="{Binding ElementName=textbox2, Path=Text}"/>
<Separator/>
<MenuItem Header="Exit..." Command="{Binding MyExitCommand}"/>
</MenuItem>
有绑定,我想查看 "textbox2"
中的文件
<TextBox x:Name="textbox2" DockPanel.Dock="Left"
Grid.IsSharedSizeScope="True"
AcceptsReturn="True"/>
您必须将 TextBox
绑定到文本文件的内容。
以下示例使用接受委托的可重用 RelayCommand
。这使得将结果传递给绑定源更加优雅。
ViewModel.cs
public class ViewModel : INotifyPropertyChanged
{
public ICommand OpenCommand => new RelayCommand(ExecuteOpemFile);
public ICommand ExitCommand => new RelayCommand(param => Application.Current.MainWindow.Close());
private string textFileContent;
public string TextFileContent
{
get => this.textFileContent;
set
{
this.textFileContent= value;
OnPropertyChanged();
}
}
public ExecuteOpemFile(object commandParameter)
{
OpenFileDialog op = new OpenFileDialog();
op.Filter = "textfile|*.txt";
op.DefaultExt = "txt";
if(op.ShowDialog() == true)
{
this.TextFileContent = File.ReadAllText(op.FileName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
RelayCommand.cs
从 Microsoft Docs: Relaying Command Logic.
复制的实现
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute) : this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion // ICommand Members
}
MainWindow.xaml
<Window>
<Window.DataContext>
<ViewModel />
</Window.DataContext>
<StackPanel>
<Menu>
<MenuItem Command="{Binding OpenCommand}" />
<MenuItem Command="{Binding ExitCommand}" />
</Menu>
<TextBox Text="{Binding TextFileContent}" />
</Window>
我这里有一个小问题,我必须在基本的 WPF 记事本之类的东西中打开(读取)一个文本文件,但我必须处理 ICommand 界面。问题是,当我选择要打开的 txt 文件时,什么也没有发生,我只看到一个空的记事本。有什么解决办法吗?这是代码:
public class OpenCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
OpenFileDialog op = new OpenFileDialog();
op.Filter = "textfile|*.txt";
op.DefaultExt = "txt";
if(op.ShowDialog() == true)
{
File.ReadAllText(op.FileName);
}
}
}
也许 bindig 不是我现在真的不知道。
<MenuItem Header="File" >
<MenuItem Header="New"/>
<MenuItem Header="Open..." Command="{Binding MyOpenCommand}" CommandParameter="{Binding ElementName=textbox2, Path=Text}"/>
<MenuItem Header="Save..." Command="{Binding MySaveCommand}" CommandParameter="{Binding ElementName=textbox2, Path=Text}"/>
<Separator/>
<MenuItem Header="Exit..." Command="{Binding MyExitCommand}"/>
</MenuItem>
有绑定,我想查看 "textbox2"
中的文件<TextBox x:Name="textbox2" DockPanel.Dock="Left"
Grid.IsSharedSizeScope="True"
AcceptsReturn="True"/>
您必须将 TextBox
绑定到文本文件的内容。
以下示例使用接受委托的可重用 RelayCommand
。这使得将结果传递给绑定源更加优雅。
ViewModel.cs
public class ViewModel : INotifyPropertyChanged
{
public ICommand OpenCommand => new RelayCommand(ExecuteOpemFile);
public ICommand ExitCommand => new RelayCommand(param => Application.Current.MainWindow.Close());
private string textFileContent;
public string TextFileContent
{
get => this.textFileContent;
set
{
this.textFileContent= value;
OnPropertyChanged();
}
}
public ExecuteOpemFile(object commandParameter)
{
OpenFileDialog op = new OpenFileDialog();
op.Filter = "textfile|*.txt";
op.DefaultExt = "txt";
if(op.ShowDialog() == true)
{
this.TextFileContent = File.ReadAllText(op.FileName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
RelayCommand.cs
从 Microsoft Docs: Relaying Command Logic.
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute) : this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion // ICommand Members
}
MainWindow.xaml
<Window>
<Window.DataContext>
<ViewModel />
</Window.DataContext>
<StackPanel>
<Menu>
<MenuItem Command="{Binding OpenCommand}" />
<MenuItem Command="{Binding ExitCommand}" />
</Menu>
<TextBox Text="{Binding TextFileContent}" />
</Window>