WPF自定义按钮
WPF custom button
我开始学习WPF
,还有一些我还不清楚的地方:
我为 button
创建了新样式:
<!-- no border button style -->
<Style x:Key="NoBorderButton" TargetType="Button">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="true">
<Setter Property="Control.FontSize" Value="18" />
</Trigger>
<Trigger Property="Control.IsMouseOver" Value="true" >
<Setter Property="Foreground" Value="LightSkyBlue" />
</Trigger>
<Trigger Property="Control.IsMouseOver" Value="false" >
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
这是我的按钮:
<Button Content="Button" Style="{StaticResource NoBorderButton}">
</Button>
现在在搜索删除所有边框的解决方案后,我发现这个 template
需要添加到 button
:
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter Content="{TemplateBinding Content}"/>
</ControlTemplate>
</Button.Template>
所以我有几个问题:
- 这个
template
在做什么?
- 为什么我不能将它添加到我在
Windows.Resources
中创建的 style
?
我将尝试直接回答您的问题,但是这里可以讨论的内容很多。
What this template doing?
所有控件都有某种默认模板,即控件外观的预定义外观。
Template
正在覆盖您的 Button
的默认 外观和感觉。您实际上正在做的是完全重新开始一个新的按钮模板。
因此,例如,您可以为按钮的外观定义一个新模板。例如,它可以是 Ellipse
中的 TextBlock
。而不是默认按钮模板。
很难用语言表达,但我想我已经解释得很好了。
Why i cannot add it the the style i created inside my Windows.Resources?
您可以:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
...
</ControlTemplate>
</Setter.Value>
</Setter>
我开始学习WPF
,还有一些我还不清楚的地方:
我为 button
创建了新样式:
<!-- no border button style -->
<Style x:Key="NoBorderButton" TargetType="Button">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="true">
<Setter Property="Control.FontSize" Value="18" />
</Trigger>
<Trigger Property="Control.IsMouseOver" Value="true" >
<Setter Property="Foreground" Value="LightSkyBlue" />
</Trigger>
<Trigger Property="Control.IsMouseOver" Value="false" >
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
这是我的按钮:
<Button Content="Button" Style="{StaticResource NoBorderButton}">
</Button>
现在在搜索删除所有边框的解决方案后,我发现这个 template
需要添加到 button
:
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter Content="{TemplateBinding Content}"/>
</ControlTemplate>
</Button.Template>
所以我有几个问题:
- 这个
template
在做什么? - 为什么我不能将它添加到我在
Windows.Resources
中创建的style
?
我将尝试直接回答您的问题,但是这里可以讨论的内容很多。
What this template doing?
所有控件都有某种默认模板,即控件外观的预定义外观。
Template
正在覆盖您的 Button
的默认 外观和感觉。您实际上正在做的是完全重新开始一个新的按钮模板。
因此,例如,您可以为按钮的外观定义一个新模板。例如,它可以是 Ellipse
中的 TextBlock
。而不是默认按钮模板。
很难用语言表达,但我想我已经解释得很好了。
Why i cannot add it the the style i created inside my Windows.Resources?
您可以:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
...
</ControlTemplate>
</Setter.Value>
</Setter>