简体   繁体   English

WPF MVMVM使用行为创建对话框

[英]Wpf Mvvm creating a Dialog using a behavior

I'm currently reviewing a Wpf Mvvm project. 我目前正在审查Wpf Mvvm项目。 I noticed that in some places new dialogs are beeing created using a behavior. 我注意到在某些地方使用行为创建了新对话框。

Lets first take a look at the code: In the view a buttons properties are set like this. 首先让我们看一下代码:在视图中,按钮属性设置如下。

  <Button Grid.Column="0"
          behaviors:OpenFileClickBehavior.IsOpenFileButton="True"
          behaviors:OpenFileClickBehavior.FileOpenDialogFilter="All files (*.*)|*.*" />

And then there's a static behavior class: 然后是一个静态行为类:

 public static class OpenFileClickBehavior
 {
        public const string IsOpenFileButtonPropertyName = "IsOpenFileButton";

        public static readonly DependencyProperty IsOpenFileButtonProperty = DependencyProperty.RegisterAttached(
        IsOpenFileButtonPropertyName,
        typeof(bool),
        typeof(OpenFileClickBehavior),
        new UIPropertyMetadata(false, OnIsOpenFileButtonPropertyChanged));

        public static readonly DependencyProperty OpenFileDialogFilterProperty = DependencyProperty.RegisterAttached(
        "OpenFileDialogFilterProperty",
        typeof(String),
        typeof(OpenFileClickBehavior),
        new UIPropertyMetadata(String.Empty));

        private static void OnIsOpenFileButtonPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
        {
            Button button = dpo as Button;
            if (button != null)
            {
                if ((bool)args.NewValue)
                {
                    button.Click += ButtonClick;
                }
                else
                {
                    button.Click -= ButtonClick;
                }
            }
        }

     private static void ButtonClick(object sender, RoutedEventArgs e)
    {
        Button button = sender as Button;
        Attachment attachment = button.DataContext as Attachment;
        String filter = GetFileOpenDialogFilter(button);

        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = filter;

        bool? dialogResult = ofd.ShowDialog();

        if (dialogResult != null && dialogResult == true)
        {
            attachment.Path = ofd.FileName;
        }
    }

So the button click event is getting registered/unregistered and handled in this class. 因此,该类中的按钮单击事件正在注册/取消注册和处理。

I know mvvm suggests viewmodels shouldn't know the view and the codebehind files of a view should be empty. 我知道mvvm建议视图模型不应该知道该视图,并且视图的代码隐藏文件应该为空。 This solution neither has code in the codebehind of a view nor creates a view from a viewmodel. 此解决方案既没有在视图的代码背后包含代码,也没有从视图模型创建视图。 Im wondering if this approach breaks the mvvm pattern in any other way, or has any downsides. 我想知道这种方法是否以其他任何方式破坏了mvvm模式,或有任何不利之处。

Thank you advance 谢谢你提前

A behavior encapsulates pieces of functionality into a reusable component, which we later on can attach to an element in a view. 行为将功能部分封装到可重用的组件中,稍后我们可以将其附加到视图中的元素上。 Emphasis is on reusable. 重点是可重用。 One can do the same code in codebehind or perhaps directly in XAML so it is nothing magic about a behavior. 可以在代码隐藏中或直接在XAML中执行相同的代码,因此,对行为而言,这并不是什么魔术。 Behaviors also have the benefit of keeping the MVVM pattern intact, since we can move code from codebehind to behaviors. 行为还具有保持MVVM模式完整的好处,因为我们可以将代码从代码隐藏移到行为。 One example is if we want to scroll in selected item in a ListBox and the selected item is chosen from code, eg from a search function. 一个示例是,如果我们要滚动列表框中的选定项目,并且选定项目是从代码(例如从搜索功能)中选择的。 The ViewModel don´t know that the view use a ListBox to show the list so it can not be used to scroll in the selected item. ViewModel不知道该视图使用ListBox来显示列表,因此不能用于滚动所选项目。 And we don´t want to put code in the codebehind, but if we use a behavior we solve this problem and creates a reusable component which can be used again. 而且,我们不想将代码放在后面的代码中,但是如果我们使用某种行为,则可以解决此问题,并创建可重复使用的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM