WPF 将 ControlTemplate 的内容绑定到控件中的属性?
我想将按钮 ControlTemplate 中的 Border.Background 绑定到按钮的背景属性。通常我会使用 TemplateBinding:
<Style TargetType="Button" x:Key="ColuredButton">
<Setter Property="Background" Value="LightGreen"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="Border" CornerRadius="2" BorderThickness="1" BorderBrush="Gray">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="{TemplateBinding Foreground}"/>
<GradientStop Color="{TemplateBinding Background}"/>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但我收到错误:“如果不在模板中,则无法设置 TemplateBinding”..但我在模板中! (如果我不使用 LinearGradientBrush 并将边框 Backround 属性直接绑定到 {TemplateBinding Background} ,它就可以工作......
I want to bind Border.Background in my Button ControlTemplate to the Background Property of my button. Usually I would use a TemplateBinding:
<Style TargetType="Button" x:Key="ColuredButton">
<Setter Property="Background" Value="LightGreen"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="Border" CornerRadius="2" BorderThickness="1" BorderBrush="Gray">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="{TemplateBinding Foreground}"/>
<GradientStop Color="{TemplateBinding Background}"/>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
But I get the error: "Cannot set TemplateBinding if not in a Template".. But I am in a Template! (It works if I dont use LinearGradientBrush and bind the borders Backround Property directly to {TemplateBinding Background}....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如@Snowbear所说,您应该将
Color
绑定到Color
,而不是将Color
绑定到Brush
。但在他的解决方案中,不允许具有深层属性Path
(例如Foreground.Color
)的TemplateBinding
作为绑定标记的一部分。所以使用以下...
它应该可以工作。
As @Snowbear said, you should bind
Color
toColor
and notColor
toBrush
. But in his solution,TemplateBinding
with deep propertyPath
such asForeground.Color
isnt allowed as part of the binding markup.So use the following...
And it should work.
我认为你可能在这里有一些其他错误,但没有得到很好的报告。
GradientStop
接受其相应属性中的Color
,而Button
的Background
和Foreground
属性> 是画笔,不是颜色。如果您认为Background
和Foreground
将是SolidColorBrush
,您可以尝试在绑定中访问它们的Color
属性,但是我不确定它是否会起作用:I think you might have some other error here, but it is not reported well.
GradientStop
accepts aColor
in its corresponding property whileBackground
andForeground
properties of aButton
are brushes, not colors. If you think thatBackground
andForeground
will beSolidColorBrush
you might try accessing theirColor
property in your binding, but I'm not sure whether it will work or not: