WPF控件的样式

时间:2014-06-20 22:43:01

标签: c# wpf

我有一个WPF Win表单应用程序。我想将样式应用于WPF中的所有控件。 例如,我当前的按钮看起来像 enter image description here

<Button  Content="Add" Width="147" Height="31" Margin="490,10,10,10" />

我想设置按钮的样式,类似于以下(带有弯曲边框)enter image description here

如何对所有控件执行类似的样式。

2 个答案:

答案 0 :(得分:0)

首先,让我们澄清您的要求。我的合格猜测是,您正在寻找带圆角的WPF按钮的样式解决方案,如我的计算器应用程序中所示:http://webinfocentral.com/VOLTA/Manual.aspx 如果是这样,则至少有以下两种解决方案:

创建ResourceDictionary文件并在WPF XAML中引用它

添加按钮样式,如下面的代码片段所示,它产生圆角(注意属性<Border.CornerRadius>2</Border.CornerRadius&gt;)和3个特定于正常外观的视觉/渐变效果,鼠标悬停和按下按钮状态:< / p>

<Style TargetType="Button" x:Key="Button_Default">
    <Setter Property="Foreground" Value="#202020"/>
<Setter Property="FontSize" Value="12" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="ButtonBackground" BorderBrush="#606060">
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    <Border.BorderThickness>0</Border.BorderThickness>
                    <Border.CornerRadius>2</Border.CornerRadius>
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#f5f5f5" Offset="0" />
                            <GradientStop Color="#c5c5c5" Offset="0.93" />
                            <GradientStop Color="#606060" Offset="0.93" />
                            <GradientStop Color="#404040" Offset="1" />
                        </LinearGradientBrush>
                    </Border.Background>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                        <Setter TargetName="ButtonBackground" Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
                                    <GradientStop Color="#f5f5f5" Offset="0" />
                                    <GradientStop Color="#c5c5c5" Offset="0.81" />
                                    <GradientStop Color="#606060" Offset="0.81" />
                                    <GradientStop Color="#404040" Offset="1" />
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="FontWeight" Value="Bold"/>
                               <Setter TargetName="ButtonBackground" Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
                                    <GradientStop Color="#e5e5e5" Offset="0" />
                                    <GradientStop Color="#dfdfdf" Offset="0.75" />
                                    <GradientStop Color="#606060" Offset="0.75" />
                                    <GradientStop Color="#303030" Offset="1" />
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

注意:与您的案例相关,您可以添加属性:

<Setter Property="Height" Value="147"/>
<Setter Property="Width" Value="31"/>
<Setter Property="Margin" Value="490,10,10,10"/>

将按钮引用添加到XAML中的Style,如下所示:

或者,您可以通过添加对ResourceDictionary的引用将样式添加到C#中的按钮,如下所示(在此示例中,您必须指定/使用按钮名称):

[ButtonName].Style = this.Resources["Button_Default"] as Style;

希望这会有所帮助。 Rgds,Alex

答案 1 :(得分:-1)

您可以使用此代码。它可能会有所帮助。

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication8.Window1"
x:Name="Window"
Title="Window1"
Width="640" Height="480">
<Window.Resources>
    <ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}">
        <Border Height="30" Width="150" BorderBrush="RoyalBlue" BorderThickness="2">
            <Border.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="WhiteSmoke" Offset="0.347"/>
                    <GradientStop Color="#FF7D91C0" Offset="0.656"/>
                </LinearGradientBrush>
            </Border.Background>
            <ContentPresenter TextElement.FontSize="15" TextElement.FontWeight="Bold"  HorizontalAlignment="Center" VerticalAlignment="Center"/>


        </Border>
    </ControlTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <Button Content="OK" Margin="172,43,269,0" VerticalAlignment="Top" Template="{DynamicResource ButtonControlTemplate1}"/>
</Grid>