简体   繁体   English

Win7上的C#WPF VS2013无法在文本框中显示所选文件名

[英]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.

相关问题 globalKeyboardHook返回意外的字符C#,VS2013 Win7 - globalKeyboardHook returns unexpected chars C#, VS2013 Win7 从C#VS2013中找到win 7中的安装路径(那里有exe文件,但当前未运行) - find the installation path (with exe file there but not currently running) in win 7 from C# VS2013 C#WPF多线程无法从VS2013中的Dispatcher.BeginInvoke返回实际值 - C# WPF multithreading cannot return the actual value from Dispatcher.BeginInvoke in VS2013 SQLite3.dll不能在VS2013 ON WIN 7中的C#应用​​程序中使用 - SQLite3.dll cannot be used in C# application in VS2013 ON WIN 7 在Win7上在dot net visual studio 2013上从C#中的xml文件加载xml文件时出错 - Error of loading data from xml file in C# on dot net visual studio 2013 on win7 从 C# VS2013 WPF 通过多线程将图像添加到地图的错误 - error of adding an image to a map by multithreading from C# VS2013 WPF 基于来自 C# VS2013 WPF 的 OpenStreetMap 在地图上显示 100 万个带有标记的位置 - show 1 million locations with markers on a map based on OpenStreetMap from C# VS2013 WPF 文件无法删除,并且复制和移动在C#VS2013中不起作用 - a file cannot be deleted and copy and move do not work in C# VS2013 无法从C#VS2013访问IBM Netezza服务器,但在IBM Aginity工作台中运行良好 - cannot access IBM Netezza server from C# VS2013 but worked well in IBM Aginity workbench 如何在VS2013中从WinForm生成日志(C#) - How to generate logs from a winform in VS2013(c#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM