wpf - 使用MVVM和WPF通过OpenFileDialog使用路径更新文件名的文本

标签 wpf mvvm textbox openfiledialog

我有一个带有路径的文件名文本框。用户使用OpenFileDialog查找文件后,应使用文件名填充此文本框。当用户直接输入带有路径的文件名而不是从文件对话框中选择时,此文本也应起作用。

由于我正在学习MVVM,因此我很难找出如何使用文件名/路径更新文本框。我尝试了所有我能想到的。

我期望onPropertyChanged(“FilenameWithPath”)应该注意这个问题。有人可以告诉我如何处理这个问题吗?

见下面的代码

FileBrowseView.xaml

<TextBox  Height="23" HorizontalAlignment="Left" Margin="113,22,0,0" 
               Name="txtFilenameWithPath" 
               Text="{Binding Path=FilenameWithPath, 
               UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
               VerticalAlignment="Top" Width="300" />
<Button 
        Content="Browse..." 
        Height="30" 
        HorizontalAlignment="Left" 
        Margin="433,20,0,0" 
        Name="btnBrowse" 
        VerticalAlignment="Top"
        Width="142" 
        Command="{Binding Path=BrowseCommand}"  />

FileBrowseView.xaml.cs
public partial class FileBrowseView : Window
{
    public FileBrowseView()
    {
        InitializeComponent();
        DataContext = new FileBrowseViewModel();
    }


}

文件浏览模型
public class FileBrowseModel
{
  private string _filenameWithPath = string.Empty;
    public string FilenameWithPath
    {
        get { return _filenameWithPath; }
        set
        {
            if (value == _filenameWithPath)
                return;
            else
                _filenameWithPath = value;
        }
    }

}

FileBrowseViewModel
public class FileBrowseViewModel  : INotifyPropertyChanged 
{   
  private string _filenameWithPath = string.Empty;
  public string FilenameWithPath
  {
        get { return _filenameWithPath; }
        set
        {
            if (value == _filenameWithPath)
                return;
            else
                _filenameWithPath = value;
            OnPropertyChanged("FilenameWithPath"); 
        }
    }

    private ICommand _browseCommand;
    public ICommand BrowseCommand
    {
        get
        {
            if (_browseCommand == null)
                _browseCommand = new DoBrowse();
            return _browseCommand;
        }
        set
        {
            _browseCommand = value;
            OnPropertyChanged("FilenameWithPath"); 
        }
    }


    private class DoBrowse : ICommand
    {
        public bool CanExecute(object parameter) { return true; }
        public event EventHandler CanExecuteChanged;
        public void Execute(object parameter)
        {
            var filedialog = new System.Windows.Forms.OpenFileDialog();
            DialogResult fresult = filedialog.ShowDialog();

            if (fresult == System.Windows.Forms.DialogResult.OK)
            {
                 FilenameWithPath = filedialog.FileName;
                //I am trying to assign value i got from OpenFileDialog to 
                // FilenameWithPath property
                //complier says "Cannot access non static member of outer type 
                   'MyProject.FileBrowseViewModel' via 
                  nested type 'MyProject.FileBrowseViewModel.DoBrowse

                onPropertyChanged(“FilenameWithPath”);                    
            }
        }
    }

   public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

最佳答案

您只需要在命令的Execute函数中设置FileNameWithPath。 FileNameWithPath的设置方法应该调用OnPropertyChanged。您不必从命令的Execute函数中调用它。

关于wpf - 使用MVVM和WPF通过OpenFileDialog使用路径更新文件名的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7152643/

相关文章:

c# - 自动布局增加控件数量

c# - Mouse.captured Windows Phone 等效项

c# - 跟踪账户余额和信息

javascript - 将文本字段限制为仅数字的最佳方法?

wpf - 如何通过单击按钮展开 Datagrid 详细信息部分?

c# - 从 c++ dll 中的线程回调更新 WPF 图像源

android - 当 "do not keep activity"开发选项打开时,ViewModel 被清除

c# - 创建新实体时如何在加载时验证表单?

excel - Excel 中的文本框控件 - 如何在 Perl 中引用

c# - 使用 C# 在 asp.net 网页的最后一个字符中将焦点设置在文本框中