我可以在WPF中动态切换样式吗?

Mar*_*ter 1 .net wpf styles

假设我想展示一些Button和几个RadioButtons.根据RadioButton选择的内容,我想对我的应用采用不同的风格Button.这可能在WPF中吗?

Mar*_*ter 10

@Brandon的答案会奏效,但我认为这更优雅一点:

<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" />
Run Code Online (Sandbox Code Playgroud)


Bra*_*don 7

您可以在后面的代码中设置样式.

    button.Style = (Style)FindResource("NameOfYourStyle");
Run Code Online (Sandbox Code Playgroud)