如何在运行时更改元素样式?

时间:2009-12-10 01:09:27

标签: c# wpf xaml styles

我有一个元素和多个样式,如何在运行时以编程方式或通过XAML绑定在样式之间切换。

<Rectangle x:Name="fixtureControl" Style="{DynamicResource FixtureStyle_Fast}">

<!-- In the style resources. -->
<Style x:Key="FixtureStyle_Fast" TargetType="{x:Type Shape}">
    <Setter Property="Stroke" Value="Black"/>
    <Setter Property="StrokeThickness" Value="20"/>
</Style>

<Style x:Key="FixtureStyle_Good" TargetType="{x:Type Shape}">
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect Opacity=".9"
                              Direction="-90"
                              RenderingBias="Performance"
                              BlurRadius="50"
                              ShadowDepth="10" />
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="FixtureStyle_Best" TargetType="{x:Type Shape}">
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect Opacity=".9"
                              Direction="-90"
                              RenderingBias="Quality"
                              BlurRadius="50"
                              ShadowDepth="10" />
        </Setter.Value>
    </Setter>
</Style>

然后我有一些处理器改变样式的单选按钮

private void RadioButton_Click(object sender, RoutedEventArgs e) {
    if (e.Source == rdoQualityBest) {
        fixtureControl.Style = FindResource("FixtureStyle_Best") as Style;
    } else if (e.Source == rdoQualityGood) {
        fixtureControl.Style = FindResource("FixtureStyle_Good") as Style;
    } else {
        fixtureControl.Style = FindResource("FixtureStyle_Fast") as Style;
    }
}

然而,这会将样式应用于元素,而不是替换它,所以如果我应用Fast然后质量,我会同时获得边框和阴影。

2 个答案:

答案 0 :(得分:7)

这样的事情在过去对我有用(纯XAML解决方案):

<!-- Styles 1-4 defined somewhere else on your page -->
<ComboBox Name="AvailableStyles">
    <ComboBoxItem Tag="{x:Null}" IsSelected="True">None</ComboBoxItem>
    <ComboBoxItem Tag="{StaticResource Style1}">1</ComboBoxItem>
    <ComboBoxItem Tag="{StaticResource Style2}">2</ComboBoxItem>
    <ComboBoxItem Tag="{StaticResource Style3}">3</ComboBoxItem>
    <ComboBoxItem Tag="{StaticResource Style4}">4</ComboBoxItem>
</ComboBox>

<Button Content="Button" Style="{Binding ElementName=AvailableStyles, Path=SelectedItem.Tag}"/>
<CheckBox Content="Check Box" Style="{Binding ElementName=AvailableStyles, Path=SelectedItem.Tag}"/>
<RadioButton Content="Radio Button"Style="{Binding ElementName=AvailableStyles, Path=SelectedItem.Tag}"/>

希望这有帮助!

答案 1 :(得分:4)

它对我来说很好

这是我的代码:

在.xaml 中

    <Window.Resources>
    <Style x:Key="FixtureStyle_Fast" TargetType="{x:Type Shape}">
        <Setter Property="Stroke" Value="Black"/>
        <Setter Property="StrokeThickness" Value="20"/>
    </Style>

    <Style x:Key="FixtureStyle_Good" TargetType="{x:Type Shape}">
        <Setter Property="Stroke" Value="Red"/>
        <Setter Property="StrokeThickness" Value="20"/>
    </Style>

    <Style x:Key="FixtureStyle_Best" TargetType="{x:Type Shape}">
        <Setter Property="Stroke" Value="Blue"/>
        <Setter Property="StrokeThickness" Value="20"/>
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
     <Rectangle x:Name="fixtureControl" Style="{DynamicResource FixtureStyle_Fast}"/>
    <StackPanel Grid.Column="1">
    <RadioButton Name="rdoQualityBest" Content="Best" Click="RadioButton_Click" />
    <RadioButton Name="rdoQualityGood" Content="Good" Click="RadioButton_Click" />
    <RadioButton Name="rdoQualityFast" Content="Fast" Click="RadioButton_Click" />
    </StackPanel>
</Grid>
.xaml.cs

中的

private void RadioButton_Click(object sender, RoutedEventArgs e)
{
    if (e.Source == rdoQualityBest)
    {
        fixtureControl.Style = FindResource("FixtureStyle_Best") as Style;
    }
    else if (e.Source == rdoQualityGood)
    {
        fixtureControl.Style = FindResource("FixtureStyle_Good") as Style;
    }
    else
    {
        fixtureControl.Style = FindResource("FixtureStyle_Fast") as Style;
    }
}