add control to style wpf

Go To StackoverFlow.com

0

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

2012-04-04 01:19
by Tono Nam


2

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>
2012-04-04 03:53
by KodeKreachor


0

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 a a grid or a whole xaml hierarchy to a a 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.

2012-04-04 04:06
by Rhyous
Ads