为什么这不起作用:
<Button Width="200" Height="50">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Height" Value="{Binding RelativeSource={RelativeSource Self}, Path=Height}"/>
<Setter Property="Background" Value="Blue"/>
<Style.Triggers>
<Trigger Property="Button.IsPressed" Value="true">
<Setter Property="Background" Value="green"/>
<Setter Property="Height" Value="20"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Template>
<ControlTemplate>
<Canvas x:Name="MainCanvas" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<Border
Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
x:Name="Border"
CornerRadius="2"
BorderThickness="1"
Background="{TemplateBinding Background}"
BorderBrush="black">
<ContentPresenter
Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True"/>
</Border>
</Canvas>
</ControlTemplate>
</Button.Template>
Hello
</Button>
像这样:
<Button Width="200" >
<Button.Style>
<Style TargetType="Button">
<Setter Property="Height" Value="50"/>
<Setter Property="Background" Value="Blue"/>
<Style.Triggers>
<Trigger Property="Button.IsPressed" Value="true">
<Setter Property="Background" Value="green"/>
<Setter Property="Height" Value="20"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Template>
<ControlTemplate>
<Canvas x:Name="MainCanvas" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<Border
Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
x:Name="Border"
CornerRadius="2"
BorderThickness="1"
Background="{TemplateBinding Background}"
BorderBrush="black">
<ContentPresenter
Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True"/>
</Border>
</Canvas>
</ControlTemplate>
</Button.Template>
Hello
</Button>
按下按钮时我希望按钮缩小。这是自定义控件的原型,因此Style将在稍后提升到Generics.xmal文件。为什么它不会在Button ???上显示'Hello'字符串
答案 0 :(得分:2)
在将TargetType="Button"
添加到ControlTemplate标记之前,字符串“Hello”不会显示在按钮上,因为否则ContentPresenter不知道它正在处理的内容:
<ControlTemplate TargetType="Button">
至于你的样式绑定,你要做的是将属性绑定到自身 - 这没有任何意义。你想要实现什么目标?