Click here to Skip to main content
11,409,097 members (49,179 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 20: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 OriginalGriff 350
1 Sergey Alexandrovich Kryukov 310
2 Maciej Los 155
3 Peter Leow 140
4 Dave Kreskowiak 115
0 Sergey Alexandrovich Kryukov 8,676
1 OriginalGriff 6,855
2 Maciej Los 3,322
3 Abhinav S 3,238
4 Peter Leow 3,034


Advertise | Privacy | Mobile
Web02 | 2.8.150414.5 | 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