Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
[no name] 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

Use animations[^]!

Assume you have following XAML:
XML
<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:
C#
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:

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


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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900