【问题标题】:MVVM, DialogService and Dialog ResultMVVM、DialogService 和 Dialog 结果
【发布时间】:2014-11-04 22:39:05
【问题描述】:

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

Good or bad practice for Dialogs in wpf with MVVM?

显示对话框效果很好,但尽管对话框实际上正在显示,但对话框结果总是错误的。我的 DialogViewModel 目前是空的,我认为也许我需要将我的 DialogViewModel “连接”到 RequestCloseDialog 事件。是这样吗?

【问题讨论】:

    标签: c# wpf mvvm dialog modal-dialog


    【解决方案1】:

    您的 DialogViewmodel 是否实现了 IDialogResultVMHelper?并且您的 View/DataTemplate 是否具有与您的 DialogViewmodel 的命令绑定,从而引发 RequestCloseDialog?

    例如

    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>
    

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

    var dialog = new DialogViewmodel();
    var result = _dialogservice.ShowDialog("My Dialog", dialog );
    
    if(result.HasValue && result.Value)
    {
        //accept true
    }
    else
    {
        //Cancel or false
    }
    

    【讨论】:

    • 谢谢!效果很好!另外,我还了解了延迟加载的世界!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    相关资源
    最近更新 更多