steveh
Topic Author
Posts: 29
Joined: 07 Oct 2019, 12:50

Behaviour<T> + VisualStateManager - Templates not applied

23 Feb 2021, 02:36

Hi guys, I'm trying to use a custom Behaviour<T> with the VisualStateManager:
/*virtual*/ void CustomButtonBehaviour::OnAttached() /*override*/
{
    Button *pBaseButton = GetAssociatedObject();
    pBaseButton->DependencyPropertyChanged() += MakeDelegate(this, &OnPropertyChanged);
}

void CustomButtonBehaviour::OnPropertyChanged(BaseComponent*, const DependencyPropertyChangedEventArgs &rArgs)
{
    if (rArgs.prop == ContentControl::ContentProperty)
    {
        Button *pButton = GetAssociatedObject();
	if (nullptr != pButton->GetContent())
	{
	    VisualStateManager::GoToState(pButton, NSS(CustomVisualState), false);
	}
    }
}
This fails to run as the template is not yet applied. There is not a nice way from what I can see to hook in to add this behaviour. Normally in Custom Controls I'd derive from OnApplyTemplate virtual and add this behaviour here, is there an alternative for behaviours?

My workaround is to force ApplyTemplate:
void CustomButtonBehaviour::OnPropertyChanged(BaseComponent*, const DependencyPropertyChangedEventArgs &rArgs)
{
    if (rArgs.prop == ContentControl::ContentProperty)
    {
        Button *pButton = GetAssociatedObject();
	if (nullptr != pButton->GetContent())
	{
	    pButton->ApplyTemplate(); // Force apply template so visual child is set
	    VisualStateManager::GoToState(pButton, NSS(CustomVisualState), false);
	}
    }
}
But this doesn't feel clean. Any ideas? Cheers!

-Steven
 
User avatar
sfernandez
Site Admin
Posts: 2056
Joined: 22 Dec 2011, 19:20

Re: Behaviour<T> + VisualStateManager - Templates not applied

23 Feb 2021, 10:45

Hi, in situations where you need controls to have their template already applied you can explicitly call ApplyTemplate() as you did, or you can hook to Loaded event, which happens after control has been measured, so its template is ready.

Cheers.
 
steveh
Topic Author
Posts: 29
Joined: 07 Oct 2019, 12:50

Re: Behaviour<T> + VisualStateManager - Templates not applied

23 Feb 2021, 12:01

Cheers Sergio!

I tried using the Loaded event but it seemed in my case that this didn't work, I think it was due to the ContentControl initially having a binding for the content set to NULL so the inner ContentTemplate was invalid until we load into our game much later down the line.

As long as manually applying the template is fine then that's fine with me, I just thought I'd check first to see if there was a different solution. Much appreciated!

-Steven
 
User avatar
sfernandez
Site Admin
Posts: 2056
Joined: 22 Dec 2011, 19:20

Re: Behaviour<T> + VisualStateManager - Templates not applied

23 Feb 2021, 12:09

Yes, you can simply use ApplyTemplate() in this case. Thanks for the update.

Who is online

Users browsing this forum: MiBa and 1 guest