Behaviour<T> + VisualStateManager - Templates not applied
Hi guys, I'm trying to use a custom Behaviour<T> with the VisualStateManager:
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:
But this doesn't feel clean. Any ideas? Cheers!
-Steven
Code: Select all
/*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);
}
}
}
My workaround is to force ApplyTemplate:
Code: Select all
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);
}
}
}
-Steven
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Behaviour<T> + VisualStateManager - Templates not applied
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.
Cheers.
Re: Behaviour<T> + VisualStateManager - Templates not applied
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
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
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Behaviour<T> + VisualStateManager - Templates not applied
Yes, you can simply use ApplyTemplate() in this case. Thanks for the update.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 18 guests