realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

Subscribe to the Behavior event from code

18 Jun 2019, 11:06

Hi
I need to connect to the <TranslateZoomRotateBehavior Dragging = "..." /> event from code
          <DataTemplate>
            <Border x:Name="PART_NodeRoot" Width="100" Height="Auto" MinHeight="50" Background="#f0202025" CornerRadius="5" Padding="0,0,0,10"
                    BorderThickness="0.5" BorderBrush="#050505" Cursor="ScrollAll"
                    PreviewMouseLeftButtonDown="OnNodeMouseDown">              
              <i:Interaction.Behaviors>
                <TranslateZoomRotateBehavior x:Name="PART_Behavior" SupportedGestures="Translate" 
                      Dragging="OnNodeDragging"
                      X="{Binding Position.X, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                      Y="{Binding Position.Y, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Scale="1.0"/>
              </i:Interaction.Behaviors>
              
              <Grid>
                <ItemsControl ItemsSource="{Binding InConnectors}" HorizontalAlignment="Left" Margin="7,30,0,0">
                <ItemsControl ItemsSource="{Binding OutConnectors}" HorizontalAlignment="Right" Margin="0,30,7,0"/>
              </Grid>
            </Border>    
          </DataTemplate>
like this the event is not triggered:
UserControl() {
		Loaded() += [this] (BaseComponent*, const RoutedEventArgs& e) {
			if (auto border = Helper::GetParentRecursive<Border>(this)) {
				if(auto behavior = border->FindName<TranslateZoomRotateBehavior>("PART_Behavior"))  {
					behavior->Dragging() += [...]  (....)  {
						printf("Not call"); 
					};
				}
			}
		};
}
in VisualTree UserControl is inside <ItemsControl ItemsSource="{Binding InConnectors}"/>
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Subscribe to the Behavior event from code

19 Jun 2019, 19:31

Hi, there is a known issue about some objects not being correctly registered in the NameScope when they are part of a template: https://www.noesisengine.com/bugs/view.php?id=1476

While we fix this issue you can change your code to get the behaviors directly from the BehaviorCollection, something like this:
UserControl() {
    Loaded() += [this] (BaseComponent*, const RoutedEventArgs& e) {
        if (auto border = Helper::GetParentRecursive<Border>(this)) {
            auto behaviors = Interaction::GetBehaviors(border);
            auto behavior = DynamicCast<TranslateZoomRotateBehavior*>(behaviors->Get(0));
            behavior->Dragging() += [...]  (....)  {
                    printf("Dragging!"); 
                };
            }
        }
    };
}
 
realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

Re: Subscribe to the Behavior event from code

20 Jun 2019, 14:45

thanks

Who is online

Users browsing this forum: Semrush [Bot] and 17 guests