我用了两天的时间解决了silverlight的问题:一个带有样式的模板控制一个按钮。在具体按钮中,我有一个以路径为内容的画布。问题是,我希望路径填充颜色绑定到模板中的ContentControl的前景。
然而,我还不能弄清楚如何构造绑定才能到达前台。例如,如果我使用TextBlock,它将自动从样式中获取前景色。正如预期的那样,文本具有前景颜色。绑定的原因是动画控制forground,我希望它传播到路径的填充颜色。有什么想法吗?
该模板包含以下内容:
<Style x:Key="PathButtonStyle" TargetType="Button">
... Animations and state stuff
<Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" >
<ContentControl x:Name="ContentContainer"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}" />
</Border>
</Style>在我的布局中,我有以下几点:
<Button x:Name="Play" Style="{StaticResource PathButtonStyle}" >
<Canvas x:Name="PlayIcon">
<Path Data="F1M191.4839,96.1763L177.9149,106.5173L177.9149,85.9293z"
Fill="{PATH TO CONTENTCONTROLS FOREGROUND}" />
</Canvas>
</Button>我已经清理了代码,并删除了一些东西,以使其更具可读性,但我希望您能理解它背后的想法。
发布于 2011-03-25 08:37:05
由于您为按钮指定了名称,因此可以使用Binding ElementName参数:
<Button x:Name="Play" Style="{StaticResource PathButtonStyle}" >
<Canvas x:Name="PlayIcon">
<Path Data="F1M191.4839,96.1763L177.9149,106.5173L177.9149,85.9293z"
Fill="{Binding Foreground, ElementName=Play}" />
</Canvas>
</Button>发布于 2011-03-25 03:12:50
实际上,您可以将Canvas设置为Button中的内容,而不是将其放在Templete中。
<Style x:Key="PathButtonStyle" TargetType="Button">
... Animations and state stuff
<Canvas x:Name="PlayIcon">
<Path Data="F1M191.4839,96.1763L177.9149,106.5173L177.9149,85.9293z"
Fill="{TemplateBinding Foreground}" />
</Canvas>
<Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" >
<ContentControl x:Name="ContentContainer"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}" />
</Border>
</Style>如果你使用这个场景,你可以做任何你想做的事情。
发布于 2015-03-16 06:40:25
绑定到ContentControl应该会有所帮助(Silverlight5):
<Button x:Name="Play" Style="{StaticResource PathButtonStyle}" >
<Canvas x:Name="PlayIcon">
<Path Data="F1M191.4839,96.1763L177.9149,106.5173L177.9149,85.9293z"
Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=ContentControl}}" />
</Canvas>
</Button>https://stackoverflow.com/questions/3856724
复制相似问题