在WPF中创建可重用的用户控件

Max*_*Max 2 c# wpf user-controls wpf-controls

在我们使用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" />

         <!-- other fields for the rest of AddressViewModel properties-->

    </Grid>

</UserControl>
Run Code Online (Sandbox Code Playgroud)

我们可以在绑定到CustomerViewModel实例的视图中使用这样的方法

 <TextBox x:Name="Name" Text="{Binding Path=Name, UpdateSourceTrigger = PropertyChanged}" Validation.ErrorTemplate="{x:Null}" />

<AddressEditor SomeProperty="{something that points to BillingAddress}" />
<AddressEditor SomeProperty="{something that points to DeliveryAddress}" />
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?我们试图将绑定指向BillingAddress但我们找不到工作方式......

在此先感谢任何贡献,

sac*_*cha 7

是的,这应该是非常简单的,要么创建一个无形的控件DataTemplate,或者只是创建一个标准的UserControl.欺骗它将其设置DataContext为完整的Address对象

<local:AddressControl DataContext="{Binding BillingAddress}"/>
Run Code Online (Sandbox Code Playgroud)

这将允许您的新"AddressControl"具有这样的标记

<StackPanel Orientation="Vertical">
<Label Content="City"/>
<TextBox Content="{Binding City}"/>

<Label Content="ZipCode"/>
<TextBox Content="{Binding ZipCode}"/>

<Label Content="ZipCode"/>
<TextBox Content="{Binding ZipCode}"/>

<Label Content="Number"/>
<TextBox Content="{Binding Number}"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)