【问题标题】:WPF Closing a Window from the a UserControl MVVMWPF 从 UserControl MVVM 关闭窗口
【发布时间】:2020-09-04 20:31:35
【问题描述】:

我的Binding 目前有问题,因为按下Button 后,它应该关闭某个Window,但它没有。这是对情况的更详细描述:

在我的应用程序中,我有以下按钮:

按下后,会启动一个新的Window。它包含UserControl 并用作DialogBox 以确认用户想要保存所有更改。这是启动DialogBox (Window) 的代码。注意窗口是通过引用传递的:

private void OpenRegisterDialog(string changes)
    {
        window = new Window
        {
            Title = "Confirmation",
            ResizeMode = ResizeMode.NoResize,
            WindowStartupLocation = WindowStartupLocation.CenterScreen
        };

        SaveConfirmationDg = new SaveConfirmationDialog(ref window);
        window.ShowDialog();
    }

注意WindowContent 还没有设置,后面我会记录。

这就是DialogBox 的样子(按下OUINON,窗口应该正在关闭,但实际上并没有):

这是用于生成 DialogBox 的 xaml,它是嵌套在 DialogBox Window 中的 UserControl

SaveConfirmation.xaml

<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Height="200" Width="350" Background="Black">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid Grid.Row="0">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" Text="Voulez-vous enregistrer les modifications?" />
    </Grid>

    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0">
            <Button Foreground="White" Content="OUI" Command="{Binding Close}" CommandParameter="close" Margin="10,0" />
        </Grid>

        <Grid Grid.Column="1">
            <Button Foreground="White" Content="NON" Command="{Binding Close}" CommandParameter="close" Margin="10,0" />
        </Grid>
        
    </Grid>

当我使用 MVVM 模式时,我将DataContext 设置为Code-Behind 中的SaveConfirmationDialogViewModel.cs(请注意,创建的窗口作为参考传递)。

public SaveConfirmationDialog(ref Window window)
    {
        InitializeComponent();
        DataContext = new SaveConfirmationDialogViewModel(ref window);
    }

最后,这是我如何创建和分配ContentDialogBox

SaveConfirmationDialogViewModel.Cs

 class SaveConfirmationDialogViewModel : ViewModelBase
{

    public Window CurrentWindow;
    public MyICommand<string> Close { get; private set; }

    SaveConfirmationDialog SaveConfDg;

    public SaveConfirmationDialogViewModel(ref Window window)
    {
        SaveConfDg = new SaveConfirmationDialog();
        window.Content = SaveConfDg;
        window.SizeToContent = SizeToContent.WidthAndHeight;
        CurrentWindow = window;
        Close = new MyICommand<string>(ExitDialog);
    }

    private void ExitDialog(string exit)
    {
        CurrentWindow.Close();
    }
}

请注意,CurrentWindow 被分配了 window 的引用,以便能够在 Constructor 之外使用它。按下 OUINON 按钮后,应该使用 Close commandExitDialog 方法来关闭 Window 但我发现它甚至没有被调用,因为我有通过放置断点进行检查。

我认为问题可能出在 DataContext 上。有谁知道问题可能是什么 是吗?

提前致谢!

【问题讨论】:

  • 我对@9​​87654352@ 是什么感到困惑? (或者你的意思是SaveConfirmation?)另外MyICommand是如何实现的?
  • 你为什么要把对窗口的引用传递给SaveConfirmationDialogViewModel

标签: wpf xaml mvvm binding datacontext


【解决方案1】:

最后,问题真的很简单,我只需要注释掉我的Constructor 的第一行并从Code-Behind 传递SaveConfirmationDialog 对象

SaveConfirmationDialogViewModel.cs

public SaveConfirmationDialogViewModel(ref Window window, SaveConfirmationDialog SaveConfDg)
    {
        //SaveConfDg = new SaveConfirmationDialog();
        window.Content = SaveConfDg;
        window.SizeToContent = SizeToContent.WidthAndHeight;
        CurrentWindow = window;
        Close = new MyICommand<string>(ExitDialog);
    }

SaveConfirmationDialog.xaml.cs

public SaveConfirmationDialog(ref Window window)
    {
        InitializeComponent();

        //Added this as second parameter
        DataContext = new SaveConfirmationDialogViewModel(ref window, this);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多