C
C#17mo ago
Angius

✅ Avalonia — stop children from triggering parent events

<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
Orientation="Horizontal"
Background="Black"
Opacity="0.7" PointerPressed="InputElement_OnPointerPressed">
<Button Command="{Binding NewWindowCommand}">S</Button>
<Button Command="{Binding MainViewCommand}">Home</Button>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
Orientation="Horizontal"
Background="Black"
Opacity="0.7" PointerPressed="InputElement_OnPointerPressed">
<Button Command="{Binding NewWindowCommand}">S</Button>
<Button Command="{Binding MainViewCommand}">Home</Button>
</StackPanel>
with the following codebehind
private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
BeginMoveDrag(e);
}
private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
BeginMoveDrag(e);
}
makes it so that clicking on the buttons doesn't trigger their commands, but rather the click gets captured by the parent and the window gets dragged
10 Replies
JakenVeina
JakenVeina17mo ago
PointerPressed being the Avalonia-equivalent of MouseClick? seems like it must be a tunneling event, instead of bubbling? your goal here would be that clicking anywhere ELSE in the stack panel allows you to drag the whole thing, but only if it's empty space? maybe verify this with some debug logging, cause I fail to see how else this would be the case, other than a bug arguably, if the event is tunneling instead of bubbling, that in itself could be considered a bug
Angius
Angius17mo ago
Basically, yeah
JakenVeina
JakenVeina17mo ago
definitely doesn't make conventional sense see if you can debug and figure out how the even is being routed maybe button commands are tied to some OTHER event? like, WPF has MouseButtonDown and MouseButtonUp?
Angius
Angius17mo ago
Adding
if (e.Source is not StackPanel) return;
if (e.Source is not StackPanel) return;
fixed it
JakenVeina
JakenVeina17mo ago
eewwwwww if you're gonna go that route, it ought to be if (e.Source is not this) or whatever that still bugs me, though, bubbling events shouldn't work that way
Angius
Angius17mo ago
this is MainWindow not StackPanel
JakenVeina
JakenVeina17mo ago
then reference it by name
Angius
Angius17mo ago
I guess I could do (e.Source as Control) is { Name: "TopBar" }
JakenVeina
JakenVeina17mo ago
or jusy e.Source == _topBar
Angius
Angius17mo ago
_topBar doesn't exist e.Source is not StackPanel { Name: "TopBar" } works fine so I'm just gonna leave it
Want results from more Discord servers?
Add your server