我怎么可以在运行时改变元素的风格? [英] How can I change an elements style at runtime?

查看:136
本文介绍了我怎么可以在运行时改变元素的风格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元素和多种样式,我怎么在运行时的风格之间无论是编程或通过XAML绑定进行切换。

I have an element and multiple styles, how do I switch between the styles at runtime either programatically or through XAML binding.

<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>



然后我有处理不断变化的风格有些单选按钮

Then I have some radio buttons that handle changing the 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;
    }
}



不过这个样式应用于元素,而不是取代它,所以如果我申请快速那么质量,我得到两个边框和下拉阴影。

However this applies the style to the element, not replacing it, so if I apply Fast then Quality, I get both the border and the drop-shadow.

推荐答案

这样的事情有在过去的(纯XAML的解决方案)为我工作:

Something like this has worked for me in the past (a pure XAML solution):

<!-- 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}"/>



希望这有助于!

Hope this helps!

这篇关于我怎么可以在运行时改变元素的风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆