用代码改变风格

时间:2013-11-30 22:23:28

标签: c# xaml hyperlink

我有一个XAML文件,我为某些内容定义了一个超链接样式。如何使用代码将样式更改为其他样式。

具体是label3上的样式超链接..

<UserControl x:Class="FHIRCDALoader.Views.SideMenuView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" >
    <Grid>

        <StackPanel Grid.Column="0"  HorizontalAlignment="Left" Width="200" Margin="2,0,0,0" Name="stackPanel1" VerticalAlignment="Top"  >

            <StackPanel.Resources>
                <Style x:Key="TxtBlkStyle" TargetType="{x:Type Label}">
                    <Setter Property="Margin" Value="0,0,0,0" />
                    <Setter Property="FontWeight" Value="ExtraBold" />
                    <Setter Property="FontSize" Value="28" />
                    <Setter Property="Foreground" Value="#FFE7840D" />
                </Style>
                <Style x:Key="TxtBlkStyle2" TargetType="{x:Type Label}">
                    <Setter Property="Margin" Value="0,0,0,0" />
                    <Setter Property="FontWeight" Value= "Regular" />
                    <Setter Property="FontSize" Value="24" />
                    <Setter Property="Foreground" Value="#FF4C47FF" />
                </Style>
                <Style x:Key="TxtSubCommand" TargetType="{x:Type Label}">
                    <Setter Property="Margin" Value="8,0,0,0" />
                    <Setter Property="FontWeight" Value= "Regular" />
                    <Setter Property="FontSize" Value="12" />
                    <Setter Property="Foreground" Value="White" />
                </Style>
                <Style x:Key="activeLink" TargetType="{x:Type Hyperlink}">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="DarkOrange" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="False">
                            <Setter Property="Foreground" Value="White" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>

            <Label Content="Start" Name="label1" Style="{StaticResource TxtBlkStyle2}" />

            <Label Name="labelloadfile" Style="{StaticResource TxtSubCommand}">
                <Label.Content>
                    <Hyperlink NavigateUri="#" Style="{StaticResource activeLink}" Click="Hyperlink_Load_CDA_File">
                        Load CDA File
                    </Hyperlink>
                </Label.Content>
            </Label>

            <Label Content="Edit" Name="label2" Style="{StaticResource TxtBlkStyle2}"/>
            <Label Name="label3" Style="{StaticResource TxtSubCommand}">
                <Label.Content>
                    <Hyperlink NavigateUri="#" Style="{StaticResource activeLink}" Click="Hyperlink_Click_TestPage">
                        Generate New Id
                    </Hyperlink>
                </Label.Content>
            </Label>
            <Label Name="label4" Style="{StaticResource TxtSubCommand}">
                <Label.Content>
                    <Hyperlink NavigateUri="#" Style="{StaticResource activeLink}" Click="Hyperlink_Click_TestPage">
                        Generate New SetId
                    </Hyperlink>
                </Label.Content>
            </Label>

        </StackPanel>
    </Grid>
</UserControl>

2 个答案:

答案 0 :(得分:0)

以下应允许您以编程方式设置样式:

label4.Style = (Style)(this.Resources["ResourceName"]);

答案 1 :(得分:0)

我认为你可以创造另一种风格

 <Style x:Key="activeLink2" TargetType="{x:Type Hyperlink}">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="DarkOrange" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="False">
                            <Setter Property="Foreground" Value="White" />
                        </Trigger>
                    </Style.Triggers>
                </Style>

然后在您的后端,您可以将更改样式分配给给定的控件

label3.Style = (Style)Application.Current.Resources["activeLink2"];