1

我正在使用 WPF MVP。我有一个带有 DataBinding 的 UserControl。在用户交互(点击事件)之后,presenter 中的属性为空,但在下一次修改绑定时的文本时,客户类不为空。我认为有两个参考。视图的数据上下文与演示者相同。

视图的数据上下文是演示者。

      public class AddCustomerPresenter : PresenterBase<AddCustomerView>
        {
            private Customer customer;

            public Customer Customer
            {
                get { 
                    return customer ?? new Customer(); }
                set
                {
                    customer = value;
                    RaisePropertyChanged(PropertyName(() => this.Customer));
                }
            }
            /// <summary>
            /// todo: a view-khoz lehetne irni egy factoryt
            /// </summary>
            public AddCustomerPresenter()
            {
                base.View = new AddCustomerView { DataContext = this};
                View.Save += View_Save;
            }

            void View_Save(object sender, EventArgs e)
            {
                int a = 2;
            }

            public void AddToCustomers()
            {
                new UnitOfWork().CustomerRepository.Add(customer);
            }
        }

       public partial class AddCustomerView : UserControl
        {
            public event EventHandler Save;
            public AddCustomerPresenter Presenter { get { return (AddCustomerPresenter)DataContext; } }

            public AddCustomerView()
            {
                InitializeComponent();
            }

            private void Save_Click(object sender, RoutedEventArgs e)
            {
                var handler = Save;
                if (handler != null) handler(this, EventArgs.Empty);
            }
        }

      public class Customer : Notifier
        {
            private string name;
            public string Name
            {
                get { return name; }
                set
                {
                    name = value;
                    RaisePropertyChanged(PropertyName(() => this.Name));
                }
            }

            Address address;
            public Address Address
            {
                get { return address??new Address(); }
                set
                {
                    address = value;
                    RaisePropertyChanged(PropertyName(() => this.Address));
                }
            }

            string phoneNumber;
            public string PhoneNumber
            {
                get { return phoneNumber; }
                set
                {
                    phoneNumber = value;
                    RaisePropertyChanged(PropertyName(() => this.Address));
                }
            }
        }
<UserControl x:Class="RentACar.Views.AddCustomerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d">
    <Border BorderBrush="Green" BorderThickness="2">
        <DockPanel HorizontalAlignment="Center">
            <Label HorizontalAlignment="Center"
                   Content="asdf"
                   DockPanel.Dock="Top"
                   FontSize="34" />

            <Grid DataContext="{Binding Customer}" DockPanel.Dock="Top">
                <Grid.Resources>
                    <Style TargetType="Label">
                        <Setter Property="FontSize" Value="12" />
                    </Style>
                    <Style TargetType="TextBox">
                        <Setter Property="Width" Value="112" />
                        <Setter Property="HorizontalAlignment" Value="Center" />
                    </Style>
                    <Style TargetType="RowDefinition">
                        <Setter Property="Height" Value="auto" />
                    </Style>
                </Grid.Resources>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <Label Content=":" />
                <TextBox x:Name="asdf"
                         Grid.Column="1"
                         Text="{Binding Name}" />

                <GroupBox Grid.Row="2"
                          Grid.ColumnSpan="2"
                          Header="">
                    <Grid DataContext="{Binding Address}">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Label Content=":" />
                        <TextBox Grid.Column="1" Text="{Binding City}" />

                        <Label Grid.Row="1" Content=":" />
                        <TextBox Grid.Row="1"
                                 Grid.Column="1"
                                 Text="{Binding Street}" />

                        <Label Grid.Row="2" Content=":" />
                        <TextBox Grid.Row="2"
                                 Grid.Column="1"
                                 Text="{Binding StreetNumber}" />
                    </Grid>
                </GroupBox>

                <Label Grid.Row="3" Content=":" />
                <TextBox Grid.Row="3"
                         Grid.Column="1"
                         Text="{Binding PhoneNumber}" />
            </Grid>

            <Button Width="auto"
                    Margin="0 10 10 10"
                    HorizontalAlignment="Right"
                    Click="Save_Click"
                    Content="" />
        </DockPanel>
    </Border>
</UserControl>

演示: 在此处输入图像描述

4

1 回答 1

2

问题出在客户属性获取器中。

return customer ?? new Customer();

它的意思是:

if(customer != null)
{
    return customer;
}
else
{
    return new Customer();
}

直到您设置客户字段,您new Customer();每次都会获得。

但你可能想要这样的东西。

if(customer != null)
{
    return customer;
}
else
{
    customer = new Customer();
    return customer;
}

或者您可以简单地设置该字段 :) 例如在构造函数中AddCustomerPresenter,然后不必使该 getter 复杂。

它可能是:

 public Customer Customer
        {
            get 
            { 
                return customer; 
            }
            set
            {
                customer = value;
                RaisePropertyChanged(PropertyName(() => this.Customer));
            }
        }
于 2013-05-14T20:19:25.963 回答