Click here to Skip to main content
11,704,474 members (49,416 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
I am dynamically creating labels in my WPF application.On mouse over i want the label to be zoomed in and out.How can i zoom my label ?
Posted 20-Nov-12 19:45pm
Comments
Krunal R at 21-Nov-12 1:49am
   
You should try to increase and decrease the Label's font size on Zoom in and out respectively (On mouse over and mouse out event)...

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use animations[^]!

Assume you have following XAML:
<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style
            TargetType="Label">
            <Setter
                Property="RenderTransform">
                <Setter.Value>
                    <ScaleTransform x:Name="transform"></ScaleTransform>
                </Setter.Value>
            </Setter>
                <Style.Triggers>
                <EventTrigger
                    RoutedEvent="MouseEnter">
                        <BeginStoryboard
                            Name="zoomIn">
                            <Storyboard>
                                <DoubleAnimation
                                    Storyboard.TargetProperty="RenderTransform.ScaleX"
                                    To="3"
                                    Duration="00:00:00.25"></DoubleAnimation>
                                <DoubleAnimation
                                    Storyboard.TargetProperty="RenderTransform.ScaleY"
                                    To="3"
                                    Duration="00:00:00.25"></DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                </EventTrigger>
                <EventTrigger
                    RoutedEvent="MouseLeave">
                    <BeginStoryboard
                        Name="zoomOut">
                        <Storyboard>
                            <DoubleAnimation
                                Storyboard.TargetProperty="RenderTransform.ScaleX"
                                To="1"
                                Duration="00:00:00.25"></DoubleAnimation>
                            <DoubleAnimation
                                Storyboard.TargetProperty="RenderTransform.ScaleY"
                                To="1"
                                Duration="00:00:00.25"></DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Name="_stackPanel">
            <Button Click="Button_Click">Add Panel</Button>
        </StackPanel>
    </Grid>
</Window>


and an eventhandler defined as follows:
private void Button_Click(object sender, RoutedEventArgs e) {
    _stackPanel.Children.Add(new Label() {
        Content = Guid.NewGuid().ToString()
    });
}

your all set!

the implicit style defined for label will apply to all Label-instances defined under the scope of MainWindow. this can sure lead to side effects, cause every label instance, that does not specifiy an alternate style be iteslf will have this behavior. In this case you could consider defining the style in a more decent element, like the StackPanel i used...or just assign a x:Key to the style and assign it when you create the label instance in codebehind:

<Style
            x:Key="zoomLabelStyle"
            TargetType="Label">
...
</Style>

private void Button_Click(object sender, RoutedEventArgs e) {
            _stackPanel.Children.Add(new Label() {
                Content = Guid.NewGuid().ToString(), 
                Style=(Style)TryFindResource("zoomLabelStyle")
            });
        }
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 700
1 OriginalGriff 495
2 CPallini 267
3 Andy Lanng 259
4 Maciej Los 235
0 OriginalGriff 8,824
1 Sergey Alexandrovich Kryukov 8,247
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100