简体   繁体   English

WPF-窗口无法关闭

[英]WPF - Window doesn't close

I used the following answer to get a new window and be able to use the values in another ViewModel: https://stackoverflow.com/a/15512972/3793542 我使用以下答案来获取一个新窗口,并能够在另一个ViewModel中使用这些值: https : //stackoverflow.com/a/15512972/3793542

I implemented this to my application, where a user can add a new Customer. 我在应用程序中实现了此功能,用户可以在其中添加新的客户。 To do this, he/she clicks the button in the CustomerDetailsView, and a new window opens (CustomerAddView). 为此,他/她单击CustomerDetailsView中的按钮,然后将打开一个新窗口(CustomerAddView)。 Then he/she fill out the details and clicks the button in the window. 然后他/她填写详细信息,然后单击窗口中的按钮。
Now, the Customer is added, my ListBox updates just fine. 现在, 添加了Customer,我的ListBox更新就好了。 But the window doesn't close. 但是窗户没有关闭。
I was hoping that any of you can spot what's wrong, as I can't seem to be able to figure it out and it's been driving me crazy. 我希望你们中的任何一个都能发现问题所在,因为我似乎无法弄清楚它,并且这使我发疯。

The code 编码
The OpenCloseWindowBehavior as mentioned in the linked answer is the same, only renamed to WindowBehavior. 链接答案中提到的OpenCloseWindowBehavior是相同的,只是重命名为WindowBehavior。

Here's the view where the button is, CustomerDetailsView (shortened a bit): 这是按钮所在的视图,CustomerDetailsView(略有缩短):

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

The new window CustomerAddView: 新窗口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: 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: 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
    }

Well you create new instance of CustomerDetailsViewModel here: 好吧,您在这里创建CustomerDetailsViewModel的实例:

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

This instance is not related to anything and it's AddCustomerOpen property is not bound to anything either, so setting it has no effect. 该实例与任何事物都不相关,并且它的AddCustomerOpen属性也不与任何事物绑定,因此设置它无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM