简体   繁体   中英

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. 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

 <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

 <?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. Update the 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 Name="myFileNameTxtBox" Grid.Row="0" Grid.Column="1" Margin="5" 
                 Text="{Binding MyFileName}" 
                 IsReadOnly="False" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM