简体   繁体   中英

add control to style wpf

    <Style x:Key="MyButton" TargetType="{x:Type Border}">
        <Style.Resources>
            <Style TargetType="{x:Type TextBlock}">                    
                <Setter Property="FontSize" Value="24"/>
                <Setter Property="Foreground" Value="{StaticResource FontColor}"/>                    
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="Cursor" Value="Hand"/>
                <Setter Property="FontWeight" Value="DemiBold" />
                <Setter Property="Effect">
                    <Setter.Value>
                        <BlurEffect Radius="2" ></BlurEffect>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="FontWeight" Value="ExtraBold" />
                    </Trigger>                                                
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>  

I want to create my own button style that consist of a border and a textblock. How can I add a control to my style? I actually want to add an extra textblock so that I can blur the textbox that is in the back making it look like a glow.

I'm not sure exactly how you want your second blur textblock to look, but the following xaml should get you well on your way to creating the button template you're after.

Each textblock, as well as the border, have their own styles so you can style them individually and wrap them all up inside the control template of your button style:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="MainTextBlock" TargetType="{x:Type TextBlock}">
        <Setter Property="FontSize" Value="24"/>
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="FontWeight" Value="DemiBold" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="FontWeight" Value="ExtraBold" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="BlurTextBlock" TargetType="{x:Type TextBlock}">
        <Setter Property="FontSize" Value="24"/>
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="FontWeight" Value="DemiBold" />
        <Setter Property="Effect">
            <Setter.Value>
                <BlurEffect Radius="2" ></BlurEffect>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="BorderStyle" TargetType="Border">
        <Setter Property="BorderBrush" Value="Orange" />
        <Setter Property="BorderThickness" Value="2" />
    </Style>

    <Style x:Key="MyButton" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Style="{StaticResource BorderStyle}">
                        <Grid>
                            <TextBlock Style="{StaticResource MainTextBlock}" Text="{TemplateBinding Content}" />
                            <TextBlock Style="{StaticResource BlurTextBlock}" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Button Style="{StaticResource ResourceKey=MyButton}" Height="30" Width="100" Content="BUTTON" />
</Grid></Window>

The Button doesn't have a Text property, it has a Content property.

The Content property can be a string but you can't expect it to always be a string.

The problem is you want to Style the Content but you can't. It is a container. You can add an image or aa grid or a whole xaml hierarchy to aa Content property.

So you need to add a TextBlock to the Content.

<Button HorizontalAlignment="Left" Margin="190,113,0,0" VerticalAlignment="Top" Width="75">
    <Button.Content>
        <Grid>
            <TextBlock Text="Click Me!" />
            <TextBlock Text="Click Me!">
                <TextBlock.Effect>
                    <BlurEffect Radius="2" />
                </TextBlock.Effect>
            </TextBlock>
        </Grid>
    </Button.Content>
</Button>

So this doesn't seem to need a style, it just needs you to add the propert Content to the Button.Content property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM