使用命令打开窗口(WPF)

时间:2017-10-08 18:52:28

标签: wpf vb.net command

我正在尝试编写命令,以便当用户单击按钮时,会打开另一个窗口。到目前为止,我在我的XAML中有这个:

<Window.CommandBindings>
    <CommandBinding
        Command="ApplicationCommands.Open"
        CanExecute="CanOpenWindow"
        Executed="OpenWindow">
    </CommandBinding>
</Window.CommandBindings

<Button
    Content="Open Another Window"
    Command="ApplicationCommands.Open"
    CommandParameter="????">
</Button>

在代码中我有:

Private Sub OpenWindow(sender As Object, e As ExecutedRoutedEventArgs)
   Dim win As New Window
   win = DirectCast(e.Parameter, Window)
   win.Show()
End Sub

我无法弄清楚什么可以取代问号来使这项工作。

PS:我认为比我更有声望的人应该创建一个名为“指挥”的新标签。

1 个答案:

答案 0 :(得分:1)

您可以将CommandParmeter属性设置为窗口类型

<Button
    Content="Open Another Window"
    Command="ApplicationCommands.Open"
    CommandParameter="{x:Type local:Window1}">
</Button>
Private Sub OpenWindow(sender As Object, e As ExecutedRoutedEventArgs)
    Dim type = DirectCast(e.Parameter, Type)
    Dim win = DirectCast(Activator.CreateInstance(type), Window)
    win.Show()
End Sub