gpt4 book ai didi

wpf - 好的取消对话框 MVVM 模式 wpf。我该怎么做

转载 作者:行者123 更新时间:2023-12-03 23:25:18 25 4
gpt4 key购买 nike

我正在开发一个 MVVM wpf 应用程序,我需要显示带有确定取消的各种对话框。我在网上看到的很少,但看起来过于复杂,或者可能是我在挣扎。

我注意到许多使用“IDialogService”

任何人都可以指向一个链接或有一个关于如何使用 MVVM 模式实现对话框的片段吗?

多谢

最佳答案

这是一个带有确定和取消按钮的准系统对话框。我已经包含了 XAML、View 和 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;
}

}
}

查看型号

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 中有其他控件将绑定(bind)到 ViewModel 中的属性,您很可能希望您的 ViewModel 实现 INotifyPropertyChanged。

希望这可以帮助...

关于wpf - 好的取消对话框 MVVM 模式 wpf。我该怎么做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4199162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com