0

I have a custom control that is really just there for type-specific template selection:

public class MyControl : Control { }

I then have a template that is auto-applied:

<Style TargetType="MyControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="MyControl">
                <Path Data="..." Fill="{TemplateBinding Foreground}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

That works fine, I can drop a into the XAML and it shows properly.

My problem is trying to get the Foreground to show correctly when this is in a content presenter. Consider the following content presenter:

<ContentPresenter Control.Foreground="Red" Content="This Works"/>

The text shown is red. But if I make the content be a "MyControl", it doesn't - it goes up the visual tree looking for a foreground to inherit. It seems to ignore Control.Foreground. How do I get my custom control to pay attention to Control.Foreground on the content presenter?

In case it matters, this content presenter is in a control template for a TabItem, trying to get one of these "MyControl" instances to be used as a the header of the TabItem.

PatrickV
  • 2,057
  • 23
  • 30
  • What happens when you use named style instead? – XAMlMAX Apr 27 '16 at 19:02
  • I'm assuming you mean for the Style that I posted. That style is getting applied - the path is rendered correctly. It just is not the right color. – PatrickV Apr 27 '16 at 19:44
  • 1
    What I have read there seems to be an issue when working with `TemplateBinding`, Have you tried `{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Foreground}` in your `Style`? [Here is a link with explanation](http://stackoverflow.com/a/9460574/2029607) – XAMlMAX Apr 27 '16 at 20:05
  • I just tried that, it did not fix the problem. I was able to bypass the problem by using a style to handle the coloring instead of the template. Still don't know why the control template approach doesn't work. – PatrickV Apr 27 '16 at 21:21

0 Answers0