2013-05-14 29 views
1

我正在使用WPF MVP。我有一個UserControl與DataBinding。 用戶交互(單擊事件)後,演示者中的屬性爲空,但在發生綁定時在文本上進行下一次修改時,客戶類不爲空。我認爲有兩個參考。該觀點的數據上下文與主持人相同。事件後的MVP綁定屬性null

該視圖的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

+0

U可以看到它的樣本代碼。我在構造函數 – user1693057

+0

中設置了視圖的直流電,抱歉...我只是感到困惑,因爲沒有整個類......只是成員。 –

+0

它已被修復 – user1693057

回答

2

問題是顧客財產的getter。

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構造函數,然後就沒有必要有吸氣複雜。

它可能是:

public Customer Customer 
     { 
      get 
      { 
       return customer; 
      } 
      set 
      { 
       customer = value; 
       RaisePropertyChanged(PropertyName(() => this.Customer)); 
      } 
     }