Search code examples
wpfxamlcontentcontrolcontentpresenter

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


Let's say that My ContentPresenter contains a Viewbox with Paths inside instead of some text, how do I change the Color of those Paths from the Content Presenter

Example

I have this 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>

and I have the ContentPresenter in a ControlTemplate of let's say a Button:

<ControlTemplate TargetType="{x:Type local:IconButton}">
       <Border>
          <Grid>                            
              <ContentPresenter x:Name="ContentPresenterIcon" ContentSource="Icon"/>                        
          </Grid>
       </Border>
...

Assigning Icon to the ContentSource property just means that the ContentPresenter is having that Viewbox as a content.

What should I put in the Fill property of the Path Element, and which property should I change in the ContentPresenter to make its value propagate to the Fill property ?

Hope I was clear.

Update :

I desperately tried to set the ContentPresenter's TextElement.Foreground property and "Relativesource" bind the Path's Fill property to it, but that predictably didn't work.

<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>
    ...

Solution

  • Your second attempt should have worked. The reason it didn't is because you are binding to an Attached Property.

    Attached property binding requires special syntax. The output window in visual studio probably tells you that the path could not be found. This should work:

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

    The only change is the parenthesis around the Path string. For more information, see WPF Attached Property Data Binding.