Can I dynamically switch between styles in WPF?

后端 未结 2 866
情书的邮戳
情书的邮戳 2020-12-21 22:03

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 st

相关标签:
2条回答
  • 2020-12-21 22:37

    @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" />
    
    0 讨论(0)
  • 2020-12-21 22:47

    You can just set the Style in the code behind.

        button.Style = (Style)FindResource("NameOfYourStyle");
    
    0 讨论(0)
提交回复
热议问题