gpt4 book ai didi

c# - MVVM、DialogService 和对话结果

转载 作者:行者123 更新时间:2023-11-30 17:46:45 29 4
gpt4 key购买 nike

我目前正在学习 WPF/MVVM,并且一直在使用以下问题中的代码来显示使用对话框服务的对话框(包括来自 Julian Dominguez 的 bool 值更改):

Good or bad practice for Dialogs in wpf with MVVM?

显示对话框效果很好,但尽管实际上显示了对话框,但对话框结果始终为 false。我的 DialogViewModel 当前是空的,我想也许我需要将我的 DialogViewModel“连接”到 RequestCloseDialog 事件。是这样吗?

最佳答案

您的 DialogView 模型是否实现了 IDialogResultVMHelper?您的 View/DataTemplate 是否具有与引发 RequestCloseDialog 的 DialogViewmodel 的命令绑定(bind)?

例如

public class DialogViewmodel : INPCBase, IDialogResultVMHelper
{
private readonly Lazy<DelegateCommand> _acceptCommand;

public DialogViewmodel()
{
this._acceptCommand = new Lazy<DelegateCommand>(() => new DelegateCommand(() => InvokeRequestCloseDialog(new RequestCloseDialogEventArgs(true)), () => **Your Condition goes here**));
}

public event EventHandler<RequestCloseDialogEventArgs> RequestCloseDialog;

private void InvokeRequestCloseDialog(RequestCloseDialogEventArgs e)
{
var handler = RequestCloseDialog;
if (handler != null)
handler(this, e);
}
}

Dialog 控件中的任意位置:

<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" MinHeight="30">
<Button IsDefault="True" Content="Übernehmen" MinWidth="100" Command="{Binding AcceptCommand}"/>
<Button IsCancel="True" Content="Abbrechen" MinWidth="100"/>
</StackPanel>

然后你的结果应该在你的 View 模型中工作

var dialog = new DialogViewmodel();
var result = _dialogservice.ShowDialog("My Dialog", dialog );

if(result.HasValue && result.Value)
{
//accept true
}
else
{
//Cancel or false
}

关于c# - MVVM、DialogService 和对话结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25779674/

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