WPF MVVM视图在单击按钮时变为空
应用程序位于MVVM和WPF中 我有一个窗口,其中有一个提交按钮。窗口正在某个父窗口中打开。当单击ok按钮时,我已经在视图模型中注入的值将变为null 视图的格式如下:WPF MVVM视图在单击按钮时变为空,wpf,mvvm,Wpf,Mvvm,应用程序位于MVVM和WPF中 我有一个窗口,其中有一个提交按钮。窗口正在某个父窗口中打开。当单击ok按钮时,我已经在视图模型中注入的值将变为null 视图的格式如下: <UserControl> <Grid Background="{x:Null}" DataContext="{Binding Source={StaticResource AddViewModelKey}}"> <Grid.ColumnDefinitions>
<UserControl>
<Grid Background="{x:Null}" DataContext="{Binding Source={StaticResource AddViewModelKey}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="LayoutRoot" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="10" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="22"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="22"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="22"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="150"></RowDefinition>
<RowDefinition Height="0"></RowDefinition>
<RowDefinition Height="10"></RowDefinition>
</Grid.RowDefinitions>
</Grid>
<Grid Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2">
<Grid.RowDefinitions>
<RowDefinition Height="15"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Margin="0,0,10,0" Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="1">
<Button Grid.Row="1" x:Name="buttonOK" TabIndex="5" VerticalAlignment="Bottom" Content="Ok" IsDefault="True" Command="{Binding Command}" Height="22" FontSize="12" Width="90" Grid.Column="2"/>
</StackPanel>
</Grid>
</Grid>
当我点击OK按钮时,父窗口变为空。从它设置空值的地方我找不到。我正在实现RelayCommand:ICommand。请调查一下哪里错了。谢谢。就像Bizz说的。你没有将Button命令设置为AddCommand,你能给出完整的代码吗?我不知道何时调用AddViewModel(AddView视图,IAddServices服务)方法。是否调用了它?如果没有,这就是为什么父窗口变为空,它没有设置。当我单击“确定”按钮时,您将button命令设置为command,而不是AddCommands。然后默认构造函数是get called,此时我如何使用参数调用构造函数。
private AddtView _view { get; set; }
private IAddServices _service;
public RelayCommand AddCommand { get; set; }
ApplicationWindow parentWindow { get; set; }
#endregion
#region Constructor
public AddViewModel()
{
AddCommand = new RelayCommand(SaveData);
//_view = new AddAirCraftView();
//parentWindow = new ApplicationWindow("Tile") { WindowContent = _view, IsResizable = false };
// To Do
}
public AddViewModel(AddView View, IAddServices Service)
{
_view = View;
_service = Service;
parentWindow = new ApplicationWindow("Title") { WindowContent = _view, IsResizable = false };
parentWindow.CloseButtonClick += parentWindow_CloseButtonClick;
}