【问题标题】:WPF - Window doesn't closeWPF - 窗口不关闭
【发布时间】:2016-04-05 10:19:29
【问题描述】:

我使用以下答案获得了一个新窗口并能够在另一个 ViewModel 中使用这些值:https://stackoverflow.com/a/15512972/3793542

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

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

这是按钮所在的视图,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>

新窗口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
    }

客户添加视图模型:

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
    }

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    你在这里创建 new CustomerDetailsViewModel 实例:

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

    这个实例与任何东西都没有关系,它的 AddCustomerOpen 属性也没有绑定到任何东西,所以设置它没有任何效果。

    【讨论】:

    • 是的,就是这样!我现在使用我的 Messenger 来调用方法
    猜你喜欢
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多