[英]Dynamically changing value of a property specified in a style in app.xaml
I am new to WPF
. 我是WPF
新手。 Recently i encountered an issue where i have to dynamically change the font size of a label. 最近,我遇到了必须动态更改标签字体大小的问题。
To achieve this i created a test application and created a style with a key in my App.xaml
. 为此,我创建了一个测试应用程序,并在App.xaml
创建了一个带有键的样式。
Then gave this style to the label. 然后将此样式赋予标签。
Style is as below: 样式如下:
<Style x:Key="myLbl" TargetType="Label">
<Setter Property="FontSize" Value="20"/>
</Style>
Then I gave this Style
to the Label
in my Window
XAML
like : 然后,我将此Style
给Window
XAML
的Label
,例如:
<Label Name="lblDemo" Content="Test" Foreground="Black" Style="{DynamicResource myLbl}"/>
I want to change the value of the Property
in the Style
with the click of the button. 我想通过单击按钮来更改“ Style
中“ Property
的值。 I searched for it a little bit but havent found anything useful. 我搜索了一下,但是还没有找到有用的东西。 Can anyone suggest me a right possible direction. 谁能建议我一个正确的可能方向。 Any help will be appreciated. 任何帮助将不胜感激。
Thanks. 谢谢。
If you'd like to do this using XAML only, you could do it like so: 如果您只想使用XAML进行此操作,则可以这样做:
<StackPanel>
<Label Name="lblDemo" Content="Test" Foreground="Black" FontSize="20"/>
<Button x:Name="ClickMe" Content="Enlarge" />
<StackPanel.Triggers>
<EventTrigger RoutedEvent="Button.Click" SourceName="ClickMe">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="lblDemo" Storyboard.TargetProperty="FontSize">
<DiscreteDoubleKeyFrame KeyTime="00:00:00.01" Value="48" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</StackPanel.Triggers>
</StackPanel>
OR you can use good old fashioned Click
event handler in code behind with this code (using the Button
above without StackPanel.Triggers
section): 或者,您可以在此代码后面的代码中使用老式的Click
事件处理程序(使用上面的Button
,而不使用StackPanel.Triggers
部分):
private void ClickMe_OnClick(object sender, RoutedEventArgs e) { lblDemo.FontSize = 48; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.