我有一个WPF应用程序,其中有两个窗口作为视图(MVVM)。使用第一个命令绑定可以很好地工作,尽管使用第二个视图命令不会触发(两个窗口都访问相同的视图模型)。它们是否绑定到按钮或按键事件等都没关系。在属性框中,命令绑定在命令文本框中不可见,尽管我猜应该是这样。
还有另一件事要说:从第一个视图中单击按钮后,第二个窗口/视图将显示+第一个窗口仍然可见。数据上下文也将委派给第二个视图。
以下是我的代码序列:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Lobpreis.ViewModels"
xmlns:Themes="clr-namespace:microsoft.Windows.Themes;assembly=PresentationFramework.Classic" x:Class="Lobpreis.Views.LiederlisteView"
mc:Ignorable="d"
Title="LiederlisteView" WindowStyle="SingleBorderWindow" Height="450" Width="800" >
<Window.DataContext>
<vm:ShellViewModel/>
</Window.DataContext>
<Window.InputBindings>
<KeyBinding Key="Down" Command="{Binding PressKeyDown}" />
<KeyBinding Key="Up" Command="{Binding PressKeyUp}" />
</Window.InputBindings>
<Grid > etc...
这就是我处理数据上下文的方式:
LiederlisteView MeineLiederlisteView = new LiederlisteView();
MeineLiederlisteView.DataContext = this;
MeineLiederlisteView.ShowDialog();
MeineLiederlisteView.Focus();