【发布时间】:2015-07-04 10:12:28
【问题描述】:
假设我的ContentPresenter 包含一个Viewbox,其中包含Paths 而不是一些文本,我如何从Content Presenter 更改那些Paths 的颜色
示例
我有这个ResourceDictionary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Viewbox>
<Grid>
<Grid Name="backgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
<Path Data="M19.833,0L32.5,0 32.5,19.833999 52.334,19.833999 52.334,32.500999 32.5,32.500999 32.5,52.333 19.833,52.333 19.833,32.500999 0,32.500999 0,19.833999 19.833,19.833999z"
Stretch="Uniform"
Fill="?????"
Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
<Path.RenderTransform>
<TransformGroup>
<TransformGroup.Children>
<RotateTransform Angle="0" />
<ScaleTransform ScaleX="1" ScaleY="1" />
</TransformGroup.Children>
</TransformGroup>
</Path.RenderTransform>
</Path>
</Grid>
</Viewbox>
</ResourceDictionary>
我在ControlTemplate 中有ContentPresenter,比如说Button:
<ControlTemplate TargetType="{x:Type local:IconButton}">
<Border>
<Grid>
<ContentPresenter x:Name="ContentPresenterIcon" ContentSource="Icon"/>
</Grid>
</Border>
...
将Icon 分配给ContentSource 属性仅意味着ContentPresenter 将该视图框作为内容。
我应该在 Path Element 的 Fill 属性中添加什么,我应该在 ContentPresenter 中更改哪个属性以使其值传播到 Fill 属性?
希望我是清楚的。
更新:
我拼命尝试设置 ContentPresenter 的 TextElement.Foreground 属性并将“Relativesource”绑定到它的 Path 的 Fill 属性,但可以预见的是这不起作用。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Viewbox>
<Grid>
<Grid Name="backgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
<Path Data="M19.833,0L32.5,0 32.5,19.833999 52.334,19.833999 52.334,32.500999 32.5,32.500999 32.5,52.333 19.833,52.333 19.833,32.500999 0,32.500999 0,19.833999 19.833,19.833999z"
Stretch="Uniform"
Fill="{Binding Path=TextElement.Foreground, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
<Path.RenderTransform>
<TransformGroup>
<TransformGroup.Children>
<RotateTransform Angle="0" />
<ScaleTransform ScaleX="1" ScaleY="1" />
</TransformGroup.Children>
</TransformGroup>
</Path.RenderTransform>
</Path>
</Grid>
</Viewbox>
<ControlTemplate TargetType="{x:Type local:IconButton}">
<Border>
<Grid>
<ContentPresenter x:Name="ContentPresenterIcon" TextElement.Foreground="Red" ContentSource="Icon"/>
</Grid>
</Border>
...
【问题讨论】:
-
请定义“不起作用”。有什么症状?
标签: wpf xaml contentcontrol contentpresenter