[英]the selected file name cannot be displayed in the text box from C# WPF VS2013 on win7
I would like to design an interface by C# WPF in VS2013. 我想在VS2013中使用C#WPF设计一个接口。 I need to press a button and select a file. 我需要按下一个按钮并选择一个文件。 But the file name cannot be displayed in the textbox. 但是文件名无法显示在文本框中。
Here is my code: 这是我的代码:
// 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.
}
}
}
}
Now, when I press "select" button, I can open the dialog window and select a file but the file name cannot ne displayed on the textbox. 现在,当我按下“选择”按钮时,我可以打开对话框窗口并选择一个文件,但是该文件名无法显示在文本框中。
Could anyone point out where I made a mistake ? 谁能指出我在哪里弄错了?
UPDATE This is my App.xaml code 更新这是我的App.xaml代码
<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>
This is my App.config 这是我的App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
The ViewModel should implement INotifyPropertyChanged
in order to notify the UI that the Value of that a Property Changed. ViewModel应该实现INotifyPropertyChanged
,以便通知UI该属性的值已更改。 Update the MainWindowViewModel
: 更新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));
}
}
}
And Update the TextBox XAML Code: 并更新TextBox XAML代码:
<TextBox Name="myFileNameTxtBox" Grid.Row="0" Grid.Column="1" Margin="5"
Text="{Binding MyFileName}"
IsReadOnly="False" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.