2016-04-05 11 views
0

nicht schließt verwendete ich folgende Antwort ein neues Fenster zu erhalten und die Werte in einem anderen Ansichtsmodell verwenden können: https://stackoverflow.com/a/15512972/3793542WPF - Fenster nicht

ich implementiert diese auf meine Anwendung, wo ein Benutzer eine neue hinzufügen Kunde. Dazu klickt er/sie auf den Button in der CustomerDetailsView und ein neues Fenster öffnet sich (CustomerAddView). Dann füllt er/sie die Details aus und klickt auf den Button im Fenster.
Jetzt, der Kunde ist hinzugefügt, meine ListBox Updates ganz gut. Aber das Fenster schließt nicht.
Ich hatte gehofft, dass jeder von euch sehen kann, was falsch ist, da ich nicht in der Lage bin, es herauszufinden, und es macht mich verrückt.

Der Code
Die OpenCloseWindowBehavior wie in der verknüpften Antwort erwähnt ist das gleiche, nur WindowBehavior umbenannt.

Hier ist der Ansicht, in der der Knopf, CustomerDetailsView (etwas verkürzt):

<UserControl x:Class="QRM.View.CustomerDetailsView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
      xmlns:src="clr-namespace:QRM" 
      xmlns:view="clr-namespace:QRM.View" 
      xmlns:viewmodel="clr-namespace:QRM.ViewModel" 
      xmlns:helpers="clr-namespace:QRM.Helpers"> 
    <UserControl.DataContext> 
     <viewmodel:CustomerDetailsViewModel /> 
    </UserControl.DataContext> 
    <UserControl.Resources> 
     <ResourceDictionary Source="../StylesRD.xaml" /> 
    </UserControl.Resources> 
    <i:Interaction.Behaviors> 
     <!-- TwoWay binding is necessary, otherwise after user closed a window directly, it cannot be opened again --> 
     <helpers:WindowBehavior WindowType="view:CustomerAddView" Open="{Binding AddCustomerOpen, Mode=TwoWay}" /> 
    </i:Interaction.Behaviors> 

    <Grid Margin="5"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="4*"/> 
      <RowDefinition Height="auto"/> 
      <RowDefinition Height="1*"/> 
      <RowDefinition Height="auto"/> 
      <RowDefinition Height="1*"/> 
     </Grid.RowDefinitions> 

     <Grid Margin="0,5,0,0"> 
      <!-- Grid with all the details--> 
     </Grid> 

     <Line Grid.Row="1" Style="{StaticResource horizontalLineStyle}" /> 

     <StackPanel Grid.Row="2" Orientation="Vertical"> 
      <Button Command="{Binding AddCommand}" CommandParameter="True"> Add a new customer</Button> 
      <!-- More buttons--> 
     </StackPanel> 

     <Line Grid.Row="3" Style="{StaticResource horizontalLineStyle}" /> 

     <StackPanel Grid.Row="4" Orientation="Vertical"> 
      <!-- More buttons--> 
     </StackPanel>  
    </Grid>   
</UserControl> 

Das neue Fenster CustomerAddView:

<Window x:Class="QRM.View.CustomerAddView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:viewmodel="clr-namespace:QRM.ViewModel" 
     Title="Add Customer" ResizeMode="NoResize" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen"> 
    <Window.DataContext> 
     <viewmodel:CustomerAddViewModel /> 
    </Window.DataContext> 
    <Window.Resources> 
     <ResourceDictionary Source="../StylesRD.xaml" /> 
    </Window.Resources> 

    <Grid Margin="5"> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="auto" /> 
      <ColumnDefinition Width="auto" /> 
      <ColumnDefinition Width="200" /> 
     </Grid.ColumnDefinitions> 

     <!-- Form to put in new Customer's details--> 

     <Button Grid.Row="2" Grid.ColumnSpan="3" Margin="0,50,0,0" 
       Command="{Binding AddConfirmCommand}">Add this customer</Button> 
    </Grid>    
</Window> 

CustomerDetailsViewModel:

public class CustomerDetailsViewModel : INotifyPropertyChanged 
    { 
     public bool isSelected = false; 
     private Nullable<bool> isVisible = new Nullable<bool>(); 
     DBCustomer dbCustomer = new DBCustomer(); 

     #region Constructor 
     public CustomerDetailsViewModel() 
     { 
      Messenger messenger = App.Messenger; 
      messenger.Register("CustomerSelectionChanged", (Action<Customer>)(param => ProcessCustomer(param))); 
     } 
     #endregion 

     #region Add a Customer 
     private bool _addCustomerOpen; 
     public bool AddCustomerOpen { get { return _addCustomerOpen; } set { _addCustomerOpen = value; OnPropertyChanged("AddCustomerOpen"); } } 

     private RelayCommand addCommand; 
     public ICommand AddCommand 
     { 
      get { return addCommand ?? (addCommand = new RelayCommand(() => AddCustomer())); } 
     } 

     private void AddCustomer() 
     { 
      this.AddCustomerOpen = true; 
     } 

     //After pressing the button in AddView 
     private RelayCommand addDoneCommand; 
     public ICommand AddDoneCommand 
     { 
      get { return addDoneCommand ?? (addDoneCommand = new RelayCommand(() => AddCustomerDone(),() => !isSelected)); } 
     } 

     public void AddCustomerDone() 
     { 
      App.Messenger.NotifyColleagues("NewCustomer"); 
      this.AddCustomerOpen = false; 
     } 
     #endregion 

     #region PropertyChanged 
     public event PropertyChangedEventHandler PropertyChanged; 
     public void OnPropertyChanged(PropertyChangedEventArgs e) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, e); 
     } 
     private void OnPropertyChanged(string propertyName) 
     { 
      if (this.PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
     #endregion 
    } 

CustomerAddViewModel:

class CustomerAddViewModel : INotifyPropertyChanged 
    { 
     private RelayCommand addConfirmCommand; 
     DBCustomer dbCustomer = new DBCustomer(); 

     public ICommand AddConfirmCommand 
     { 
      get { return addConfirmCommand ?? (addConfirmCommand = new RelayCommand(() => AddConfirmCustomer())); } 
     } 

     private void AddConfirmCustomer() 
     { 
      if (!dbCustomer.Create(newCustomer)) 
      { 
       return; 
      } 
      CustomerDetailsViewModel _closeTheWindow = new CustomerDetailsViewModel(); 
      _closeTheWindow.AddDoneCommand.Execute(false); 
     } 

     private Customer newCustomer = new Customer(); 
     public Customer NewCustomer 
     { 
      get { return newCustomer; } 
      set { newCustomer = value; OnPropertyChanged(new PropertyChangedEventArgs("NewCustomer")); } 
     } 

     #region PropertyChanged 
     public event PropertyChangedEventHandler PropertyChanged; 
     public void OnPropertyChanged(PropertyChangedEventArgs e) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, e); 
     } 
     #endregion 
    } 

Antwort

2

Nun, Sie erstellen neue Instanz CustomerDetailsViewModel hier:

CustomerDetailsViewModel _closeTheWindow = new CustomerDetailsViewModel(); 
_closeTheWindow.AddDoneCommand.Execute(false); 

Diese Instanz nicht auf etwas bezogen ist, und es ist AddCustomerOpen Eigenschaft auch nicht an irgendetwas gebunden ist, so einstellen es hat keine Wirkung.

+0

Ja, das war es! Ich benutze jetzt meinen Messenger, um die Methode aufzurufen – Kailayla