繁体   English   中英

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

[英]creating a reusable user control in WPF

在我们使用WPF for UI构建的(相当大的)LOB应用程序中,我们有很多视图模型包含相同类型的数据子对象。 例如,有很多地址

public class AddressViewModel : INotifyPropertyChanged
{

   public string City {...}
   public string ZipCode {...}
   public string Address {...}
   public string Number {...}

  // INPC logic omitted 
}

分散在业务对象之间:

public class CustomerViewModel : INotifyPropertyChanged
{
     public string Name {...}
     public AddressViewModel BillingAddress {...}
     public AddressViewModel DeliveryAddress {...}
     /*
     ...
     */
}

是否可以构建一个可重用的自定义用户控件,我们可以绑定到任何地址子对象?

在用于编辑客户详细信息的视图(可能是另一个自定义用户控件)中,我们希望像这样放置一个自定义控件

<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>

我们可以在绑定到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}" />

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

在此先感谢您的贡献,

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

<local:AddressControl DataContext="{Binding BillingAddress}"/>

这将允许您的新“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>

您可以为adresseditor设置datacontext,例如。 只要billingaddress具有用户控件中所需的属性,它就应该可以工作

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

除了设置datacontext之外,还可以为您的usercontrol创建依赖项属性,您可以将BillingAddress绑定到该属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM