所选文件名无法显示在 Win7 上的 C# WPF VS2013 的文本框中
本文关键字:VS2013 WPF 文本 上的 Win7 文件名 显示 | 更新日期: 2023-09-27 18:37:27
我想在VS2013中设计一个由C# WPF设计的接口。我需要按下一个按钮并选择一个文件。但是文件名不能显示在文本框中。
这是我的代码:
// MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace wpf_select_file
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
// MainWidow.xaml
<Window x:Class="wpf_select_file.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpf_select_file"
Title="MainWindow" Height="150" Width="525">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="myFile" Margin="5" />
<TextBox Name="myFileNameTxtBox" Grid.Row="0" Grid.Column="1" Margin="5"
Text="{Binding myFileName}"
DataContext="{Binding myFile}"
IsReadOnly="False" />
<Button Name="SelectFile" Content="Select" Grid.Row="0" Grid.Column="2" Margin="5"
Width="50" Height="25" Command="{Binding OpenFileCommand}">
</Button>
</Grid>
// MainWindowViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace wpf_select_file
{
class MainWindowViewModel
{
private bool canExecute = true;
public ICommand openFileCommand;
public string myFileName;
public MainWindowViewModel()
{
OpenFileCommand = new RelayCommand(OpenFileCommandRun, param => this.canExecute);
}
public ICommand OpenFileCommand
{
get { return openFileCommand; }
set { openFileCommand = value; }
}
public void OpenFileCommandRun(object obj)
{
if (obj != null)
MessageBox.Show("OpenFileCommandRun " + obj.ToString());
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Title = "Select My File";
dlg.Filter = "My Files (*.csv,*.txt)|*.csv;*.txt|All Files (*.*)|*.*";
if ((bool)dlg.ShowDialog())
{
// Open document
myFileName = dlg.FileName; // I need the selected filename displayed in the textbox.
}
}
}
}
现在,当我按下"选择"按钮时,我可以打开对话框窗口并选择一个文件,但文件名无法显示在文本框中。
谁能指出我在哪里犯了错误?
更新这是我的应用代码
<Application x:Class="wpf_select_file.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
这是我的应用程序配置
。 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
视图模型应实现INotifyPropertyChanged
,以便通知 UI 该属性的值已更改。更新MainWindowViewModel
:
public class MainWindowViewModel : INotifyPropertyChanged
{
private bool canExecute = true;
public ICommand openFileCommand;
private string _myFileName;
public string MyFileName
{
get
{
return _myFileName;
}
set
{
_myFileName = value;
RaisePropertyChanged("MyFileName");
}
}
public MainWindowViewModel()
{
OpenFileCommand = new RelayCommand(OpenFileCommandRun, param => this.canExecute);
}
public ICommand OpenFileCommand
{
get { return openFileCommand; }
set { openFileCommand = value; }
}
public void OpenFileCommandRun(object obj)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Title = "Select My File";
dlg.Filter = "My Files (*.csv,*.txt)|*.csv;*.txt|All Files (*.*)|*.*";
if ((bool)dlg.ShowDialog())
{
// Open document
MyFileName = dlg.FileName; // I need the selected filename displayed in the textbox.
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
并更新文本框 XAML 代码:
<TextBox Name="myFileNameTxtBox" Grid.Row="0" Grid.Column="1" Margin="5"
Text="{Binding MyFileName}"
IsReadOnly="False" />