English 中文(简体)
• 如何在WPF控制中使用风格?
原标题:How to apply style in WPF Controls?
  • 时间:2010-05-27 11:57:25
  •  标签:
  • wpf

我是妇联的开端人,需要你们的帮助。

Problem: I have 4 buttons on the form and need to apply 2 different style on pair of 2 buttons.

是否有办法消除这一障碍?

请尽可能提供我的样本......

预告......

最佳回答

You can define named styles and then assign them explicitly to any controls as you wish. Here is a primer for styling buttons: 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>
问题回答

如果有人想在布顿直接写<编码>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>

《刑法》 不同县或任何其他州的不同风格

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

here i declaring the style for both textBlock and button.Use this one..





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签