将控件添加到样式wpf
我想创建自己的按钮样式,包括边框和文本块。如何将控件添加到样式中?实际上,我想添加一个额外的文本块,这样我可以模糊后面的文本框,使其看起来像一个光晕。我不确定您希望第二个模糊文本块的外观如何,但是下面的xaml应该可以帮助您创建所需的按钮模板 每个文本块以及边框都有自己的样式,因此您可以单独设置它们的样式,并将它们全部封装在按钮样式的控制模板中:将控件添加到样式wpf,wpf,templates,xaml,styles,Wpf,Templates,Xaml,Styles,我想创建自己的按钮样式,包括边框和文本块。如何将控件添加到样式中?实际上,我想添加一个额外的文本块,这样我可以模糊后面的文本框,使其看起来像一个光晕。我不确定您希望第二个模糊文本块的外观如何,但是下面的xaml应该可以帮助您创建所需的按钮模板 每个文本块以及边框都有自己的样式,因此您可以单独设置它们的样式,并将它们全部封装在按钮样式的控制模板中: <Style x:Key="MyButton" TargetType="{x:Type Border}"> <
<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>
按钮没有文本属性,它有内容属性 Content属性可以是字符串,但不能期望它始终是字符串 问题是你想设计内容的风格,但你不能。它是一个容器。可以向内容属性添加图像、网格或整个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>
因此,这似乎不需要样式,只需要将属性内容添加到Button.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>