如何在WPF控件中应用样式?

时间:2010-05-27 11:57:25

标签: wpf

我是WPF的初学者,需要你的帮助。

问题: 我在表单上有4个按钮,需要在2对按钮上应用2种不同的样式。

我们有什么方法可以实现这个目标吗?

请尽可能提供样品......

提前致谢...

3 个答案:

答案 0 :(得分:14)

您可以定义命名样式,然后根据需要将它们显式分配给任何控件。   这是造型按钮的底漆:Getting Started with WPF : Button Control Part 2 – Basic Styling

这是一个例子:

<Window x:Class="WpfButtonStyling.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="250" Width="400">
    <Window.Resources>
        <Style x:Key="ButtonStyle1" 
               TargetType="{x:Type Button}">
            <Setter Property="Foreground"
                    Value="Red" />
            <Setter Property="Margin"
                    Value="10" />
        </Style>
        <Style x:Key="ButtonStyle2" 
               TargetType="{x:Type Button}">
            <Setter Property="Foreground"
                    Value="Blue" />
            <Setter Property="Margin"
                    Value="10" />
        </Style>
    </Window.Resources>

    <Grid>
        <StackPanel>
            <Button x:Name="FirstButton"
                    Content="First!"
                    Style="{StaticResource ButtonStyle1}"/>
            <Button x:Name="SecondButton"
                    Content="Second"
                    Style="{StaticResource ButtonStyle2}" />
        </StackPanel>
    </Grid>
</Window>

答案 1 :(得分:1)

如果有人想直接在Button中编写Style,请按照以下方式编写:

    <Button>
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="FontFamily" Value="TimesNewRoman" />
                <Setter Property="FontSize" Value="50"/>
                <Setter Property="Background" Value="Green"/>
            </Style>
        </Button.Style>
    </Button>

答案 2 :(得分:0)

使用此代码针对不同按钮或任何其他

的不同样式
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         HorizontalAlignment="Left"
         VerticalAlignment="Top">
<Window.Resources>
    **<Style x:Key="a" TargetType="{x:Type TextBlock}">
        <Setter Property="FontFamily" Value="Verdana" />
        <Setter Property="FontSize" Value="50"/>
        <Setter Property="Background" Value="Indigo"/>
    </Style>
    <Style x:Key="b" TargetType="{x:Type TextBlock}">
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontSize" Value="16"/>
    </Style>
    <Style x:Key="c" TargetType="{x:Type Button}">
        <Setter Property="FontFamily" Value="TimesNewRoman" />
        <Setter Property="FontSize" Value="50"/>
        <Setter Property="Background" Value="Green"/>
    </Style>
</Window.Resources>
<Grid>
    <TextBlock Margin="26,41,39,0" Style="{StaticResource a}" Height="100" VerticalAlignment="Top">TextBlock with Style1</TextBlock>
    <TextBlock Margin="26,77,39,0" Height="32" VerticalAlignment="Top">TextBlock with no Style</TextBlock>
    <TextBlock Margin="26,105,67,96" Style="{StaticResource b}">TextBlock with Style2</TextBlock>
    <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="26,170,-26,0">
        <Button Style="{StaticResource c}">
            <Bold >Styles</Bold></Button>
        <Button Style="{StaticResource c}">are</Button>
        <Button Style="{StaticResource c}">cool</Button>
    </StackPanel>
</Grid>

在这里我声明了textBlock和button的样式。使用这个..