WPF - 窗口不关闭

时间:2016-04-05 10:19:30

标签: c# wpf mvvm

我使用以下答案来获取一个新窗口,并能够使用另一个ViewModel中的值:https://stackoverflow.com/a/15512972/3793542

我将此实现到我的应用程序,用户可以在其中添加新客户。为此,他/她单击CustomerDetailsView中的按钮,将打开一个新窗口(CustomerAddView)。然后他/她填写详细信息并单击窗口中的按钮 现在,添加了客户,我的ListBox更新就好了。但是窗户没有关闭。
我希望你们中的任何一个人都能发现什么是错的,因为我似乎无法弄明白这一点,这让我发疯了。

代码
链接答案中提到的OpenCloseWindowBehavior是相同的,只是重命名为WindowBehavior。

这是按钮所在的视图,CustomerDetailsView(缩短了一点):

sklearn\tree\_tree.c:24340

新窗口CustomerAddView:

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

CustomerDetailsViewModel:

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

CustomerAddViewModel:

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
    }

1 个答案:

答案 0 :(得分:2)

您可以在此处创建CustomerDetailsViewModel的 new 实例:

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

此实例与任何内容无关,并且它的AddCustomerOpen属性也未绑定到任何内容,因此设置它不起作用。