WPF (AKA Avalon)

Tuesday, September 27, 2005

Reuse a panel with help of a UserControl

This is better sample of how You can build a component like a panel for reuse purposes. The class UserControl offers a simple way to divide a page (like a dialog) or window into pieces. The programming model is analog to Window. In this case we benefit from the advantage that a UserControl can thus be used to build a composite control for reuse within your app in multiple places. A good example is the volume-panel from the last sample, again:

    1) Add an 'Avalon User Control' to your visual studio project
    2) Fill the FixedTemplate with your desired controls
    3) Add logic to the code-behind file of your control

To use this UserControl in your application add 'Mapping' to the XAML file i which you want to refer to your UserControl. Now place the new UserControl within a layout multiple times.

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>