缩放效果到WPF标签 [英] Zoom effect to wpf label
问题描述
我正在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 ?
推荐答案
使用动画 [
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()
});
}
全部准备好了!
为label定义的隐式样式将应用于在MainWindow范围内定义的所有Label实例.这肯定会导致副作用,导致每个标签实例都没有指定其他样式,因为iteslf将具有此行为.在这种情况下,您可以考虑在一个更体面的元素中定义样式,例如我使用的StackPanel ...,或者只是在样式中分配x:Key并在代码背后创建标签实例时分配它:
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")
});
}
这篇关于缩放效果到WPF标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!