Question

I am working with WPF MVP. I have a UserControl with DataBinding. After the user interaction (click event) the property in the presenter is null, but at the next modify on the text when the binding is happening, the customer class not null. I think there is two reference. The datacontext of the view is equal with the presenter.

The datacontext of the view is the 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>

Demostation: enter image description here

Was it helpful?

Solution

Problem is in Customer property getter.

return customer ?? new Customer();

it means:

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

Untill you set customer field you will get new Customer(); every time.

but you probably want something like this.

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

or you can simply set that field :) for example in constructor of AddCustomerPresenter and then it's not necessary to have that getter complicated.

it could be:

 public Customer Customer
        {
            get 
            { 
                return customer; 
            }
            set
            {
                customer = value;
                RaisePropertyChanged(PropertyName(() => this.Customer));
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top