Page 1 of 1

Behavior with Event inside Style

Posted: 05 Jun 2019, 18:16
by realesmedia
Hi
I implemented Behavior in which there is an Event. If I attach this Behavior to the element that is inside Style.ControlTemplate ..., then the event is not called. When invoking Event, the list of delegates is empty. As I understand it, when using inside Styles, you need to clone the Behavior created in Style.

How to act in this case?

In the method of
         Noesis::Ptr<Noesis::Freezable> LayoutDragElementBehavior::CreateInstanceCore() const
	{
		return *new LayoutDragElementBehavior(mDragEvent);
	}
I create a Behavior instance with an existing event. It works.
        <TabControl x:Name="tab" Visibility="Collapsed" TabStripPlacement="Bottom" BorderThickness="0.5" BorderBrush="#303030" SelectionChanged="OnTabSelectionChanged">
          <TabControl.Resources>     
        
              <Style TargetType="{x:Type TabItem}">
                <Setter Property="Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                      <Grid Height="25">
                     		<i:Interaction.Behaviors>
                         		 <docking:LayoutDragElementBehavior OnDrag="OnTabItemDrag"/>
                      		  </i:Interaction.Behaviors>
                        
                        <Border Name="Border" Background="Transparent"  BorderThickness="0" CornerRadius="0"  Margin="0"  BorderBrush="#303030">
                          <TextBlock />
                        </Border>
                      </Grid>
                    </ControlTemplate>
                  </Setter.Value>
                </Setter>
              </Style>
        
          </TabControl.Resources>
        </TabControl>

Re: Behavior with Event inside Style

Posted: 06 Jun 2019, 17:46
by sfernandez
Hi,

The Freezable class exposes the virtual method CloneCommonCore() where any inheriting class can perform their own clone code.

For example, the TriggerBase class in the Interactivity package (available in the Native SDK under Src/Packages/App/Interactivity/Src folder) uses that method to clone its collection of actions:
void NoesisApp::TriggerBase::CloneCommonCore(const Freezable* source)
{
    ParentClass::CloneCommonCore(source);

    TriggerActionCollection* thisActions = GetActions();

    TriggerBase* trigger = (TriggerBase*)source;
    TriggerActionCollection* actions = trigger->GetActions();
    int numActions = actions->Count();
    for (int i = 0; i < numActions; ++i)
    {
        Ptr<TriggerAction> action = actions->Get(i)->Clone();
        thisActions->Add(action);
    }
}
I think it is better to use the default constructor in the CreateInstanceCore method, and override the CloneCommonCore for extending the clone process.

Re: Behavior with Event inside Style

Posted: 07 Jun 2019, 08:07
by realesmedia
Thanks

Re: Behavior with Event inside Style

Posted: 10 Jun 2019, 12:56
by jsantos
Marking as solved. Thanks for your feedback.