当内容不是文本时,设置 ContentPresenter 的内容颜色

Set the ContentPresenter's content color, when the content is not a text

假设我的 ContentPresenter 包含一个 Viewbox,里面有 Paths 而不是一些文本,我如何更改这些 Paths 的颜色Content Presenter

例子

我有这个 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 将该 Viewbox 作为内容。

我应该在路径元素的填充 属性 中放入什么,我应该在 ContentPresenter 中更改哪个 属性 以使其值传播到填充 属性?

希望我说清楚了。

更新:

我拼命尝试设置 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>
    ...

你的第二次尝试应该成功了。它没有的原因是因为你绑定到 Attached Property.

附加的 属性 绑定需要特殊语法。 visual studio 中的输出 window 可能告诉您找不到路径。这应该有效:

Fill="{Binding Path=(TextElement.Foreground), RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 

唯一的变化是 Path 字符串两边的括号。有关详细信息,请参阅 WPF Attached Property Data Binding