|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWPF enables developers with even the most limited design skills to create highly-polished user interfaces, but the process may involve a long series of frustrating trial-and-error experiments using unfamiliar design tools, borrowing secret sauce from (usually randomly selected) online demos, and the generation of large volumes of XAML markup filled with indecipherable references like “Data=’M81.719619,26.720311 C41.467481,9.9004151 42.081815,10.203673 1.3438841,26.022972 8.820712,-17.102152 81.334525,-9.4161124 81.719619,26.720311 z’”, with results that sometimes get distorted when sizes or color schemes change from what was specified in the original demo. The attached project lets you quickly try out a wide range of production quality variations on common UI effects (rollover “inner glow”, button affordance, “glass” highlights…), which technically COULD be applied directly to a shipping product just by referencing the custom control DLL and setting button properties, but is actually intended to demonstrate some interesting WPF Custom Control implementation issues.
What's Covered
Button ConstructionThe default <ControlTemplate TargetType="{x:Type local:XSButton}">
<Border Name="OuterBorder" CornerRadius="{Binding CornerRadius,
ElementName=InnerBorder}"
BorderBrush="{TemplateBinding OuterBorderBrush}"
BorderThickness="{TemplateBinding OuterBorderThickness}" >
<Border Name="InnerBorder" Background="{TemplateBinding Background}"
CornerRadius="{TemplateBinding CornerRadius}"
BorderBrush="{TemplateBinding InnerBorderBrush}"
BorderThickness="{TemplateBinding InnerBorderThickness}" >
<Grid >
<Border Name="Glow" Opacity="0" CornerRadius="{Binding CornerRadius,
ElementName=InnerBorder}">
<Border.Background>
<RadialGradientBrush>
<GradientStop Offset="0" Color="{Binding GlowColor,
RelativeSource={RelativeSource TemplatedParent},
Converter={StaticResource ColorToAlphaColorConverter},
ConverterParameter=176}" />
<GradientStop Offset="1" Color="{Binding GlowColor,
RelativeSource={RelativeSource TemplatedParent},
Converter={StaticResource ColorToAlphaColorConverter},
ConverterParameter=0}" />
</RadialGradientBrush>
</Border.Background>
</Border>
<Border Name="padding" Margin="{TemplateBinding Padding}"
HorizontalAlignment="Center" VerticalAlignment="Center">
<ContentPresenter Name="content" />
</Border>
<Control Name="Highlight"
Template="{TemplateBinding HighlightAppearance}" />
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="OuterBorder" Property="Opacity" Value="0.9" />
<Setter TargetName="InnerBorder" Property="Opacity" Value="0.9" />
<Setter TargetName="content" Property="Margin" Value="2,2,0,0" />
<Setter TargetName="Glow" Property="Opacity" Value="0.5" />
<Setter TargetName="Highlight" Property="Opacity" Value="0.5" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True" >
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource GlowOn}" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource GlowOff}" />
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
private static void OnHighlightStyleChanged
( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
XSButton btn = (XSButton)d;
Highlight highlight = (Highlight)e.NewValue;
// Assign style associated with user-selected enum value
btn.Style = (Style)btn.TryFindResource
( new ComponentResourceKey( btn.GetType(), highlight.ToString() ) );
}
Drawbacks and LimitationsThere is much more that we could add to our custom button. For example, a custom Another option would be to encapsulate the code in a Other Projects by Andy L.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||