简体   繁体   English

将控件添加到样式 wpf

[英]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.我不确定您希望第二个模糊文本块的外观如何,但以下 xaml 应该可以帮助您顺利创建所需的按钮模板。

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:每个文本块以及边框都有自己的 styles,因此您可以单独设置它们的样式并将它们全部包装在按钮样式的控件模板中:

<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. Button 没有 Text 属性,它有 Content 属性。

The Content property can be a string but you can't expect it to always be a string. Content 属性可以是一个字符串,但您不能期望它总是一个字符串。

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.您可以将图像或网格或整个 xaml 层次结构添加到内容属性。

So you need to add a TextBlock to the Content.因此,您需要在 Content 中添加一个 TextBlock。

<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.所以这似乎不需要样式,它只需要您将属性 Content 添加到 Button.Content 属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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