Listings 1: XAML • Reuse Business Object Layouts

User controls are a flexible way to provide entity specifc fragments in an application. Restricting the user control to layout and binding lets you reuse visual styling and behavior supplied in the App.xaml global resource file. WPF grids provide layout similar to HTML table layout.

<UserControl x:Class="Customer2" xmlns= 
"http://schemas.microsoft.com/winfx/2006/xaml/
presentation"
xmlns:support=
	"clr-namespace:KadGen.WpfSupport;assembly=WpfSupport"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<UserControl.CommandBindings>
	<CommandBinding Command="Save"
		CanExecute="SaveCanExecute"
		Executed="SaveExecuted"/>
</UserControl.CommandBindings>
<UserControl.Resources>
	<DataTemplate x:Key="CustomerTemplate">
		<TextBlock Text="{Binding Path=LastName}"/>
	</DataTemplate>
</UserControl.Resources>
<support:ValidationDecorator>
	<support:AuthorizationDecorator 
		Name="AuthorizationDecorator">
		<Grid>
			<Grid.RowDefinitions>
				<RowDefinition Height="70"/>
				<RowDefinition Height="Auto"/>
				<RowDefinition Height="Auto"/>
				<RowDefinition Height="Auto"/>
				<RowDefinition Height="Auto"/>
			</Grid.RowDefinitions>
			<Grid.ColumnDefinitions>
				<ColumnDefinition Width="Auto"/>
				<ColumnDefinition/>
			</Grid.ColumnDefinitions>
			<Label Grid.Row="0" Grid.Column="0" 
				Target="{Binding 
				ElementName=SelectListBox}">
				<AccessText>Select _Customer</AccessText>
			</Label>
			<ListBox Name="SelectListBox" Margin="5" 
				ItemsSource="{Binding}"
				Grid.Row="0" Grid.Column="1"
				IsSynchronizedWithCurrentItem="True"
				ItemTemplate="{StaticResource 
				CustomerTemplate}"/>
			<Label Grid.Row="1" Grid.Column="0" 
				Target="{Binding 
				ElementName=FirstNameTextBox}" >
				<AccessText>_First Name</AccessText>
			</Label>
			<TextBox Name="FirstNameTextBox" Margin="3" 
				Grid.Row="1" Grid.Column="1" 
				Text="{Binding Path=FirstName}"/>
			<Label Grid.Row="2" Grid.Column="0"
				Target="{Binding 
				ElementName=LastNameTextBox}" >
				<AccessText>_Last Name</AccessText>
			</Label>
			<TextBox Name="LastNameTextBox"  Margin="3"
				Grid.Row="2" Grid.Column="1" 
				Text="{Binding Path=LastName}"/>
			<Label Grid.Row="3" Grid.Column="0" 
				Target="{Binding 
				ElementName=GenderComboBox}" >
				<AccessText>_Gender</AccessText>
			</Label>
			<ComboBox Name="GenderComboBox" 
				Margin="3" Grid.Row="3" Grid.Column="1" 
				Text="{Binding Path=Gender}"
				Style="{StaticResource EnumCombo}"/>
			<StackPanel Grid.Row="4" Grid.Column="0" 
				Grid.ColumnSpan="2" Margin="0,20,0,0" 
				HorizontalAlignment="Center">
			<Button Name="HelpButton" Padding="30,0,30,0"
				Click="HelpButton_Click" >Help</Button>
			<Button Padding="30,0,30,0" Command="Save"
				Content="{Binding 
				RelativeSource={RelativeSource Self}, 
				Path=Command.Text}" />
			<StackPanel Orientation="Horizontal" >
			<RadioButton Margin="5,10,5,10" 
				Click="Clerical_Click">Clerical</RadioButton>
			<RadioButton Margin="5,10,5,10" 
				Click="Manager_Click">Manager</RadioButton>
			</StackPanel>
			</StackPanel>
		</Grid>
	</support:AuthorizationDecorator>
</support:ValidationDecorator>
</UserControl>
comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.