按钮单击时,WPF MVVM视图变为空

时间:2014-03-18 13:07:00

标签: wpf mvvm

应用程序在MVVM和WPF中

我有一个提交按钮的窗口。窗口在某个父窗口中打开。单击确定按钮时,我已在视图模型中注入的值变为空。

视图采用以下格式:

 <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>

在视图模型中我有:

    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;

    }

当我单击“确定”按钮时,父窗口变为空。从它设置null的地方我无法找到。我正在实现RelayCommand:ICommand。请查看错误的地方。感谢。

1 个答案:

答案 0 :(得分:0)

就像Bizz说的那样。你没有将Button命令设置为AddCommand,你能提供完整的代码吗?我不知道什么时候调用AddViewModel(AddView View,IAddServices服务)方法。它被调用?如果没有,那么为什么你的父窗口变为null,它没有设置。