文件浏览器对话框MVVM C#,返回完整的路径以查看模型并仅显示视图中的文件名

时间:2018-10-09 15:51:35

标签: c# wpf mvvm

我有一个对话框窗口,我需要将完整的文件路径返回到我的viewmodel,我想这样做,但是只在文本框中为用户显示文件名。我有一个行为类和一个viewmodel和一个视图。我尝试在行为类中将名称捕获为属性,但是绑定到文本框以检查输出时没有得到该值。这是浏览器对话框

namespace BasemapCreator.Behaviors
{
    public class FolderDialogBehavior:Behavior<System.Windows.Controls.Button>
    {

        public void FolderBrowserDialog()
        {


        }
        public string SetterName { get; set; }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Click += OnClick;
        }
        protected override void OnDetaching()
        {

            AssociatedObject.Click -= OnClick;
        }
        private void OnClick(object sender, RoutedEventArgs e)
        {

            var dialog = new FolderBrowserDialog();
            var result = dialog.ShowDialog();
            if (result == DialogResult.OK && AssociatedObject.DataContext != null)
            {
                var propertyInfo = AssociatedObject.DataContext.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public)
                    .Where(p => p.CanRead && p.CanWrite)
                    .First(p => p.Name.Equals(SetterName));

                string dirName = new DirectoryInfo(dialog.SelectedPath).Name;
                FolderName = dirName;
                _fileName = System.IO.Path.GetFileName(dirName);
                FileName = _fileName;
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(FileName.ToString());
                propertyInfo.SetValue(AssociatedObject.DataContext, dialog.SelectedPath, null);
            }
        }
        private string _folderName;
        public string FolderName
        {
            get { return _folderName; }
            set
            {
                if (value != _folderName )
                {
                    _folderName = value;
                    OnPropertyChanged("FolderName");
                }
            }

        }
        private string _fileName;
        public string FileName
        {
            get { return _fileName; }
            set
            {
                if (_fileName != value)
                {
                    _fileName = value;
                    OnPropertyChanged("FileName");
                }
            }

        }
        private string _styleName;
        public string StyleName
        {
            get { return _styleName; }
            set
            {
                if (_styleName != value)
                {
                    _styleName = value;
                    OnPropertyChanged("StyleName");
                }
            }
        }

        private void OnPropertyChanged(string propertyname)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyname));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, args);
        }

    }
}

这是视图中的绑定

<TextBox x:Name="gdbName" HorizontalAlignment="Left" Height="30" Margin="56,29,0,0"  Text="{Binding FolderName, Mode=TwoWay}" VerticalAlignment="Top" Width="282" AllowDrop="True">
        </TextBox>

在ViewModel中,我具有FolderName属性

private string _folderName;
public string FolderName
{
   get { return _folderName; }
   set
      {
        _folderName = value;
       OnPropertyChanged("FolderName");
      }
}      

一切正常,但是路径名很长,以至于有时它们被隐藏了。我想显示文件名并传递完整路径。

1 个答案:

答案 0 :(得分:0)

如果显示缩短的路径或文件名,但让用户输入完整路径,则不可能或至少非常复杂……

我将使用两个不同的字段或使用OneWay绑定,并让用户仅使用FileOpenDialog选择一个新文件,而不让他手动输入路径…

因此,您可以在视图模型中创建另一个属性,例如:

public string SortenedFolderName => Path.GetFileName(_folderName);

private string _folderName;
public string FolderName
{
   get { return _folderName; }
   set
      {
        _folderName = value;
       OnPropertyChanged(nameof(FolderName));
       OnPropertyChanged(nameof(SortenedFolderName));
      }
} 

并绑定到此属性,例如:

<TextBox Text="{Binding SortenedFolderName, Mode=OneWay}" IsReadOnly="True" />