WPF (AKA Avalon)

Monday, September 19, 2005

Reuse a panel with help of a template (XAML)

Here is a short sample which demonstrates how to build a panel for reuse purposes with a ContentTemplate (completely in XAML). Another way would be to code one XAML-file encapsulating the panel and refer to this panel when needed.
OK, now here's what I did:
 
<ControlTemplate x:Key="myContent">
<Border Margin="4" Padding="4"
HorizontalAlignment="Center"
BorderBrush="Silver"
BorderThickness="1"
CornerRadius="5">
<DockPanel HorizontalAlignment="Center">
<TextBlock HorizontalAlignment="Center"
DockPanel.Dock="Top" Text="DefaultText" />
<Button DockPanel.Dock="Top" Content="Check 1" />
<Button DockPanel.Dock="Top" Content="Check 2" />
<Button DockPanel.Dock="Top" Content="Check 3" />
<DockPanel DockPanel.Dock="Top" HorizontalAlignment="Center">
<Button DockPanel.Dock="Left" Content="On" />
<Button DockPanel.Dock="Right" Content="Off" />
</DockPanel>
<Slider Orientation="Vertical" DockPanel.Dock="Top"
HorizontalAlignment="Center" Height="100" />
<TextBlock HorizontalAlignment="Center" DockPanel.Dock="Top" Text="Line 1" />
<TextBlock HorizontalAlignment="Center" DockPanel.Dock="Top" Text="Line 2" />
<TextBlock HorizontalAlignment="Center" DockPanel.Dock="Top" Text="Line 3" />
<Button DockPanel.Dock="Top" Content="Normalize" />
<Button DockPanel.Dock="Top" Content="Invert" />
<Button DockPanel.Dock="Top" Content="Print" />
<Slider Orientation="Horizontal" Value="5" Minimum="0" Maximum="10" />
</DockPanel>
</Border>
</ControlTemplate>

After defining this template (could be anything e.g. a mixer bar) in the Resources of the Window one can use it several times:

<DockPanel LastChildFill="False">
<ContentControl Template="{StaticResource myContent}" />
<ContentControl Template="{StaticResource myContent}" />
<ContentControl Template="{StaticResource myContent}" />
<ContentControl Template="{StaticResource myContent}" />
<ContentControl Template="{StaticResource myContent}" />
</DockPanel>

0 Comments:

Post a Comment

<< Home