Why can't I click / click the empty areas inside the border / ContentControl without setting the transparency of the child background?

Finally, I was able to create an “easy” transparent button control based on ContentControl . However, can someone explain why I could not click / touch any empty areas of the control until I set the background transparency of the child? I ran into this problem when:

  • I tried using Border
  • I install a ControlTemplate button, not a ContentTemplate.

Here is my "button" class:

 public class TransparentButton : ContentControl { public TransparentButton() { HorizontalContentAlignment = HorizontalAlignment.Stretch; } public override void OnApplyTemplate() { var child = Content as Grid; if (child != null) { child.Background = new SolidColorBrush(Colors.Transparent); } base.OnApplyTemplate(); } } 

This is quite specific for my use cases (assuming it's a grid child), but it works. The reason I use this is because lists (non-ListBox) with TiltEffect enabled.

Problem Context:

 <ItemsControl x:Name="Items" toolkit:TiltEffect.IsTiltEnabled="True"> <ItemsControl.ItemTemplate> <DataTemplate> <controls:TransparentButton cal:Message.Attach="[Event Tap] = [Action Go($dataContext)]"> <Grid> <StackPanel HorizontalAlignment="Left"> <TextBlock Text="{Binding Test}" /> </StackPanel> <StackPanel HorizontalAlignment="Right"> <TextBlock Text="{Binding Test2}" /> </StackPanel> </Grid> </controls:TransparentButton> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 

If you click between StackPanels inside an element, the event will not be fired and nothing will happen. Only when the background of the grid is Transparent , does it "take up space".

I come from the web background, so this is confusing; the containing element must be “verifiable”, even if it is not installed.

+3
source share
1 answer

An object without a background is usually called hollow or not allowed in XAML terms. Thus, it is necessary to set the background so that the object responds to hits. To achieve a result test for a transparent object, you must set the background to transparent.

Additional Impact Testing Information

http://msdn.microsoft.com/en-us/library/ms752097.aspx

+12
source

Source: https://habr.com/ru/post/1397012/


All Articles