[C#]所选的文件名不能显示在从 C# WPF VS2013 上 win7 的文本框

标签: Visual-Studio WPF C#
发布时间: 2017/2/24 1:34:48
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

设计由 C# VS2013 的 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.    
        }
      }
   } 
}

现在,当我按"选择"按钮,我可以打开对话窗口,并选择一个文件但文件名称不能显示在文本框中的 ne。

任何人都能指出弄错的地方吗?

更新 这是我的 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>

这是我的 App.config

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
 </startup>

解决方法 1:

ViewModel 应执行 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));
        }
    }
}

和更新的 TextBox XAML 代码︰

   <TextBox Name="myFileNameTxtBox" Grid.Row="0" Grid.Column="1" Margin="5" 
                 Text="{Binding MyFileName}" 
                 IsReadOnly="False" />
赞助商