事件后的MVP Binded属性为null

时间:2013-05-14 19:35:56

标签: c# .net wpf silverlight mvp

我正在使用WPF MVP。我有一个带DataBinding的UserControl。 在用户交互(单击事件)之后,演示者中的属性为null,但在绑定发生时对文本进行下一次修改时,customer类不为null。我认为有两个参考。视图的datacontext与演示者相同。

视图的datacontext是演示者。

      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>

Demostation: enter image description here

1 个答案:

答案 0 :(得分: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));
            }
        }