我有银色问题我用两天时间来打架:带有样式的模板控制按钮。在具体按钮中,我有一个以路径为内容的画布。问题是我希望路径填充颜色从模板中的ContentControl绑定到Foreground。
但是,我还没有弄清楚如何构建绑定以获取Forground。如果我愿意,例如使用TextBlock,它将自动从Style中获取Forground颜色。正如预期的那样,文本具有前景色。绑定的原因是动画控制着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>
我已经清理了代码并删除了一些内容以使其更具可读性,但我希望您能够了解它背后的想法。
答案 0 :(得分:9)
由于您为按钮指定了名称,因此可以使用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>
答案 1 :(得分:1)
实际上,不是将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>
如果您将使用此方案,您可以执行任何操作。
答案 2 :(得分:0)
绑定到ContentControl应该有帮助(Silverlight 5):
<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>