katit
katit

Reputation: 17915

Create template-less user control. OnApplyTemplate

I have "Service" custom control that doesn't need to be visual. I just add it to my view so it can bind to VM and perform some functionality.

Style looks like so:

<Style TargetType="controls:IdattInteractions">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="controls:IdattInteractions">
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

When view initialized I don't get OnApplyTemplate called on my control. Is that because ControlTemplate empty? How do I achieve functionality I need? I need to proble visual tree around this control for some functionality and I wanted to do it inside OnApplyTemplate.

How should I go about this?

Upvotes: 0

Views: 485

Answers (2)

Mike Post
Mike Post

Reputation: 6460

I'm guessing you "add it to your view" by declaring as a resource. If that's the case, the reason you never receive a call to OnApplyTemplate is because your control is not part of the visual tree. (Test it out: in the debugger, break at the end of your constructor. You'll have a ResourceDictionary with your control living inside. Try to keep following the Parent property until you reach your control. You'll find the Parent will be null quite quickly within the hierarchy.)

To get a call from OnApplyTemplate, you'll need to add your custom control into the root layout container of some other control. Then you'll be inside the visual tree.

Upvotes: 0

Maxim V. Pavlov
Maxim V. Pavlov

Reputation: 10509

As far as I know, if your "control" is not visual, then it should be a business logic object of some class, running in memory and interacting with the ViewModel if needed. No need to declare it in XAML.

Upvotes: 1

Related Questions