1

我对 WPF、XAML 和数据绑定比较陌生。我有一个视图(窗口)和一个视图模型。

我试图实现 MVVM 模式,这意味着视图和视图模型都没有相互引用。所有数据交换都是通过数据绑定发生的。

到目前为止一切顺利,但现在我遇到了一个我找不到解决方案的问题。

在我看来,我有一个绑定到命令的按钮开始。

<Button Command="{Binding NextCommand}" Content="Next">

NextCommand 是类型ActionCommand : ICommand

在我的例子中,NextCommand 只是在视图模型中调用了一个私有方法。

到目前为止我找不到解决方案的问题如下:

如何在视图模型 NextCommandAction 方法结束时关闭窗口?

private void NextCommandAction(object o)
{
    ...
    ...
    // close the window
}

由于我没有对视图的引用,因此我不能只设置DialogResult = true;

到目前为止,我发现的唯一可行的解​​决方案是将隐藏单选按钮添加到视图并将其值绑定到属性 CloseView 并在 xaml.cs 文件中创建方法 CloseView,该方法绑定到隐藏单选的 Checked 事件-按钮。在该方法中,我设置 DialogResult = true;

虽然这可行,但我觉得必须有一个比在视图中添加隐藏元素更好的解决方案!

4

4 回答 4

0

您可以将窗口引用作为 CommandParameter 传递给 Close 命令,并在窗口上执行任何需要的操作。

<Button Content="Close" Command="{Binding Path=CloseCommand}" 
  CommandParameter="{Binding ElementName=Window}"/>

private void CloseCommand(object sender)
{
    Window wnd = sender as Window;
    wnd.Close();
}
于 2014-10-06T06:14:41.157 回答
0

CommandParameter="{Binding ElementName=Window}"假定您的 XAML 中有一个名为“Window”的元素。例如,您的 Window 标签需要Name="Window"

于 2016-10-31T13:53:31.907 回答
0

这个问题是当我用谷歌搜索检查是否DialogResult是依赖属性时首先出现的问题之一(它不是:-))

将依赖属性添加到您的窗口:

 public static readonly DependencyProperty InteractionResultProperty =
            DependencyProperty.Register(
                nameof(InteractionResult),
                typeof(Boolean?),
                typeof(MyWpfWindow1),
                new FrameworkPropertyMetadata(default(Boolean?), 
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                    OnInteractionResultChanged));

  public Boolean? InteractionResult
        {
            get => (Boolean?) GetValue(InteractionResultProperty);
            set => SetValue(InteractionResultProperty, value);
        }

        private static void OnInteractionResultChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((MyWpfWindow1) d).DialogResult = e.NewValue as Boolean?;
        }

我将我的属性命名为 InteractionResult,虽然一个好名字也可以。

在 xaml 之后,您可以将其与样式绑定


 <Window.Style>
        <Style TargetType="{x:Type z:MyWpfWindow1}">
            <Setter Property="InteractionResult"
                    Value="{Binding UpdateResult}" />
        </Style>
    </Window.Style>

UpdateResult 是我的视图模型中的属性。

  private Boolean? _updateResult;

        public Boolean? UpdateResult
        {
            get => _updateResult;
            set => SetValue(ref _updateResult, value);
        }

SetValue 方法是通常的通知属性

 protected virtual Boolean SetValue<T>(ref T field, T value, 
            [CallerMemberName]String propertyName = null)
        {
            if (Equals(field, value))
                return false;

            field = value;

            RaisePropertyChanged(propertyName);
            return true;
        }

并且该属性以通常的方式设置

<Button Content="Cancel" 
        Command="{Binding CancelCommand}" />

ICommand CancelCommand { get; }


private void OnCancel()
{
   UpdateResult = false;
}

免责声明:适用于我的电脑。

于 2020-04-07T07:27:03.673 回答
-1

受 Chandrashekhar Joshi 回答的启发(但不使用元素的名称):

在 Button 中定义 CommandParameter:

<Button
  Command="{Binding CloseCommand}"
  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
  Content="Close" />

定义命令(和实现):

CloseCommand = new DelegateCommand<Window>((w) => w.DialogResult = true);
于 2021-01-27T12:00:12.557 回答