在我们使用WPF for UI构建的(相当大的)LOB应用程序中,我们有许多包含相同类型的数据子对象的视图模型.例如,有很多地址
public class AddressViewModel : INotifyPropertyChanged
{
public string City {...}
public string ZipCode {...}
public string Address {...}
public string Number {...}
// INPC logic omitted
}
Run Code Online (Sandbox Code Playgroud)
分散在业务对象中:
public class CustomerViewModel : INotifyPropertyChanged
{
public string Name {...}
public AddressViewModel BillingAddress {...}
public AddressViewModel DeliveryAddress {...}
/*
...
*/
}
Run Code Online (Sandbox Code Playgroud)
是否可以构建一个可重用的自定义用户控件,我们可以绑定到任何地址子对象?
在用于编辑客户详细信息的视图(可能是另一个自定义用户控件)中,我们希望像这样放置一个自定义控件
<UserControl x:Class="OurApp.View.AddressEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox x:Name="ZipCode" Text="{Binding Path=ZipCode, UpdateSourceTrigger = PropertyChanged}" Validation.ErrorTemplate="{x:Null}" HorizontalAlignment="Left" Height="23" Margin="10,19,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" /> …Run Code Online (Sandbox Code Playgroud)