确定取消对话框MVVM模式wpf.我该怎么做 [英] Ok cancel Dialog MVVM Pattern wpf.How can I do it

查看:93
本文介绍了确定取消对话框MVVM模式wpf.我该怎么做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MVVM wpf应用程序上工作,我需要显示各种带有ok cancel的对话框.我在网上看到的很少,但是看起来过于复杂,或者可能是我在挣扎.

I am working on a MVVM wpf application and I need to show various dialogs with ok cancel. I have seen few on the net but the look overcomplicated or may be its me struggling.

我注意到许多使用"IDialogService"的人

I Have noticed that many that use "IDialogService"

任何人都可以将我指向一个链接,或者是否有关于如何使用MVVM模式实现对话框的摘录?

Can anybody point me to a link or has a snippet handy on how to implement a Dialog using a MVVM Pattern?

非常感谢

推荐答案

这是带有确定"和取消"按钮的准系统对话框.我包括了XAML,View和ViewModel:

here is a barebones dialog with OK and Cancel buttons. I have included the XAML, View, and ViewModel:

XAML:

<Window
    x:Class="TestProject.Views.OKCancelDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
    Title="Window Title"
    Height="300"
    Width="600"
    WindowStartupLocation="CenterOwner"
    WindowStyle="ToolWindow"
    ResizeMode="CanResize"
    UseLayoutRounding="True"
    TextOptions.TextFormattingMode="Display">

    <Grid>
        <Button
            Name="OKButton"
            Content="OK"
            Height="23"
            HorizontalAlignment="Right" 
            Margin="0,0,93,12"
            VerticalAlignment="Bottom" 
            Width="75" 
            Click="OKButton_Click"
            IsDefault="True"
            Command="{Binding OKButtonCommand}" />

        <Button
            Name="CancelButton"
            Content="Cancel" 
            Height="23" 
            HorizontalAlignment="Right" 
            Margin="0,0,12,12"
            VerticalAlignment="Bottom"
            Width="75" 
            IsCancel="True" />
    </Grid>
</Window>

隐藏代码:

using System.Windows;
using TestProject.ViewModel;

namespace TestProject.Views
{
    public partial class OKCancelDialog : Window
    {
        private readonly OKCancelViewModel viewModel;

        //I use DI to inject the ViewModel into the View
        //This will allow you to use the view for different ViewModels if you need to.
        //Create an Interface for your ViewModel to implement to make ViewModel unit testing
        //easier. Testing frameworks such as rhino mock require Interfaces to test methods
        //in a class under test and it allows you to use an IoC Container to create the
        //ViewModel for you.                 
        public OpenReturnDialog(IOKCancelViewModel viewModel)
        {
            InitializeComponent();
            this.viewModel = viewModel; //Do this if you need access to the VM from inside your View. Or you could just use this.Datacontext to access the VM.
            this.DataContext = viewModel;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

    }
}

ViewModel

using Microsoft.Practices.Composite.Presentation.Commands;


namespace TestProject.ViewModel
{
    public class OKCancelViewModel
    {
        public OKCancelViewModel()
        {
            OKButtonCommand = new DelegateCommand<object>(HandleOKButtonCommand, CanExecuteOKButtonCommand);
        }

        public DelegateCommand<object> OKButtonCommand { get; private set; }

        public void HandleOKButtonCommand(object obj)
        {
             //Here is where your code for OK button clicks go.

        }

        public bool CanExecuteOKButtonCommand(object obj)
        {
            //Put code to determine when the OK button will be enabled/disabled.
        } 

        //You may want to add a command for the Cancel button also if you have a need to 
        //enable/disable the cancel button
        //The command will look just like the OK button command above.
    }
}

现在,如果您的UI中有其他控件绑定到ViewModel的属性,那么您很可能希望ViewModel实现INotifyPropertyChanged.

Now, you will most likely want to have your ViewModel implement INotifyPropertyChanged in the event you have other controls in your UI that will bind to properties in the ViewModel.

希望这对您有帮助...

Hope this helps...

这篇关于确定取消对话框MVVM模式wpf.我该怎么做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆