【问题标题】:add control to style wpf将控件添加到样式 wpf
【发布时间】:2012-04-04 01:19:48
【问题描述】:
<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>
我想创建自己的包含边框和文本块的按钮样式。如何为我的样式添加控件?我实际上想添加一个额外的文本块,这样我就可以模糊后面的文本框,使它看起来像一个发光体。
【问题讨论】:
标签:
wpf
templates
xaml
styles
【解决方案1】:
我不确定您希望第二个模糊文本块的外观如何,但以下 xaml 应该可以帮助您顺利创建所需的按钮模板。
每个文本块以及边框都有自己的样式,因此您可以单独设置它们的样式并将它们全部包装在按钮样式的控件模板中:
<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>
【解决方案2】:
Button 没有 Text 属性,它有 Content 属性。
Content 属性可以是字符串,但您不能期望它始终是字符串。
问题是您想为内容设置样式但您不能。它是一个容器。您可以将图像或网格或整个 xaml 层次结构添加到 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>
所以这似乎不需要样式,它只需要您将属性 Content 添加到 Button.Content 属性。