私はWPF MVPを使用しています。DataBinding を使用した UserControl があります。ユーザー インタラクション (クリック イベント) の後、プレゼンターのプロパティは null ですが、バインディングが発生しているときにテキストを次に変更すると、顧客クラスは null ではありません。参考書は2つあると思います。ビューのデータコンテキストはプレゼンターと同じです。
ビューのデータコンテキストはプレゼンターです。
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>
デモステーション: