本文介绍了我可以在WPF中动态切换样式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要显示一个 Button 和一些 RadioButtons 。根据选择的 RadioButton ,我想对 Button 应用不同的样式。在WPF中是否可能?

Let's say I wanted to display a Button and a few RadioButtons. Based on which RadioButton is selected, I want to apply a different style to my Button. Is this possible in WPF?

推荐答案

@Brandon的答案会起作用,但我认为这稍微有些优雅:

@Brandon's answer would have worked, but I think this is a little more elegant:

<ComboBox Name="AvailableStyles">
    <ComboBoxItem Tag="{x:Null}" IsSelected="True">None</ComboBoxItem>
    <ComboBoxItem Tag="{StaticResource FirstStyle}" Style="{StaticResource FirstStyle}">Style 1</ComboBoxItem>
    <ComboBoxItem Tag="{StaticResource SecondStyle}" Style="{StaticResource SecondStyle}">Style 2</ComboBoxItem>
    <ComboBoxItem Tag="{StaticResource ThirdStyle}" Style="{StaticResource ThirdStyle}">Style 3</ComboBoxItem>
</ComboBox>

<Button Style="{Binding ElementName=AvailableStyles, Path=SelectedItem.Tag}"  Content="Dynamically Styled Button" />

这篇关于我可以在WPF中动态切换样式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:04