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.
source share