2

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へのバインディングを指定しようとしましたが、機能する方法が見つかりませんでした...

貢献してくれてありがとう、

4

2 に答える 2

8

ええ、それは非常に簡単なはずです。 でルックレス コントロールを作成するか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>
于 2013-01-31T13:54:17.147 に答える