MVVM C#,将属性从ViewModel移至Model类

时间:2018-10-14 07:58:01

标签: c# mvvm

我有绑定到SetterName的文件夹浏览器对话框

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;
                propertyInfo.SetValue(AssociatedObject.DataContext, dialog.SelectedPath, null);
            }
        }

我在ViewModel中设置了属性

public class CommonUseWindowViewModel:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public Model New { get; set; }
        public ICommand Build { get; set; }
        public CommonUseWindowViewModel()
        {
            Build = new DelegateCommand(ClickedMethod);

        }

        protected async void ClickedMethod()
        {

            IsEnabled = false;
            var gdbName = FolderName;
            var styleFol = StyleName;
            var envArray = await QueuedTask.Run(() => Geoprocessing.MakeEnvironmentArray(overwriteoutput: true));
            var valueArray = await QueuedTask.Run(() => Geoprocessing.MakeValueArray(gdbName, styleFol));
            string toolPath = @"c:\staging\ProBaseMapBuilder\BasemapBuilder.tbx\BasemapCreator";
            var gpresult1 = await QueuedTask.Run(() => Geoprocessing.ExecuteToolAsync(toolPath, valueArray, envArray));
            MessageBox.Show("All Layers Have Been Added to The Map");
            IsEnabled = true;
        }
        private bool _isEnabled = true;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                OnPropertyChanged("IsEnabled");
            }
        }
        private string _folderName;
        public string ShortenedFolderName => Path.GetFileName(_folderName);
        public string FolderName
        {
            get { return _folderName; }
            set
            {
                _folderName = value;
                OnPropertyChanged("FolderName");
                OnPropertyChanged(nameof(ShortenedFolderName));
            }
        }

        private string _styleName;
        public string ShortenedStyleName => Path.GetFileName(_styleName);
        public string StyleName
        {
            get { return _styleName; }
            set
            {
                _styleName = value;
                OnPropertyChanged("StyleName");
                OnPropertyChanged(nameof(ShortenedStyleName));
            }
        }

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

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

    }

我想将属性移至Model类,但是当我执行文件夹对话框时,出现一个错误,即没有匹配的设置器名称。我认为问题来自于不知道如何将其绑定到视图。我想知道我是否在视图中正确引用了命名空间

        xmlns:FolderDialog="clr-namespace:BasemapCreator.Behaviors"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:Behaviors="clr-namespace:ArcGIS.Desktop.Internal.Framework.Behaviors;assembly=ArcGIS.Desktop.Framework" x:Class="BasemapCreator.CommonUseWindow" 
        xmlns:DataContext="clr-namespace:BasemapCreator.Models"



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

当我将Model.FolderName添加到文本框绑定中时,我收到来自文件夹对话框的错误。

这是我的模特

namespace BasemapCreator.Models
{
    public class Model:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private bool _isEnabled = true;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                OnPropertyChanged("IsEnabled");
            }
        }
        private string _folderName;
        public string ShortenedFolderName => Path.GetFileName(_folderName);
        public string FolderName
        {
            get { return _folderName; }
            set
            {
                _folderName = value;
                OnPropertyChanged("FolderName");
                OnPropertyChanged(nameof(ShortenedFolderName));
            }
        }

        private string _styleName;
        public string ShortenedStyleName => Path.GetFileName(_styleName);
        public string StyleName
        {
            get { return _styleName; }
            set
            {
                _styleName = value;
                OnPropertyChanged("StyleName");
                OnPropertyChanged(nameof(ShortenedStyleName));
            }
        }

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

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

    }
}

我正在使用通过单击按钮创建的视图模型 按钮类

namespace BasemapCreator
{
    internal class ShowWindow : Button
    {
        private CommonUseWindow _dlg = null;
        protected override void OnClick()
        {
            if (_dlg != null) return;
            _dlg = new CommonUseWindow();
            _dlg.Closing += ProWin_Closing;
            _dlg.Owner = FrameworkApplication.Current.MainWindow;
            _dlg.Show();
        }
        void ProWin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _dlg = null;
        }
    }
}

在视图后面的代码中,我已将DataContext设置为viewModel,可以更改它并仍然使用viewmodel吗?

using ArcGIS.Desktop.Framework.Controls;
using BasemapCreator.ViewModels;
using System;
using System.Windows;
using BasemapCreator.Models;

namespace BasemapCreator
{
    /// <summary>
    /// Interaction logic for CommonUseWindow.xaml
    /// </summary>
    public partial class CommonUseWindow : ProWindow
    {
        private CommonUseWindowViewModel _vm = new CommonUseWindowViewModel();
        public CommonUseWindow()
        {
            InitializeComponent();
            this.DataContext = _vm;
        }

    }
}

以下是该视图的代码,

1 个答案:

答案 0 :(得分:0)

在您的MainWindow代码后面...

using YourProjectName.FolderName;

然后在MainWindow构造函数中

InitializeComponent();
//usually this is the only code behind in a view
DataContext = new ClassName();