有没有一种方法可以让我点击 WrapPanel 的子项并获取子项,而不必在每个子项中插入 Click 事件?我可以只在 WrapPanel 上插入一个 Click 事件吗?
我的代码如下:
<ScrollViewer Name="scrollViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" Height="500" Margin="1085,154,-89,0" HorizontalAlignment="Left" Width="267" VerticalAlignment="Top">
<WrapPanel Name="Agenda" HorizontalAlignment="Left" Height="Auto" VerticalAlignment="Top" Width="265" Background="#FFEEF4FF">
<Border Name="b06x00" BorderThickness="1" Width="265" BorderBrush="Black" Visibility ="Visible">
<TextBlock TextWrapping="Wrap" Text="06:00" Width="265" Height="15" Background="White"/>
</Border>
...
this Border + TextBlock is repeated 70 times. It's an agenda by the way.
...
</WrapPanel>
</ScrollViewer>
最佳答案
例如,您可以在 WrapPanel
上处理 MouseDown
事件,因为它会从该面板中的任何控件冒泡
<WrapPanel Name="Agenda" ... MouseDown="Agenda_MouseDown">
在事件处理程序中,您可以像这样检查 OriginalSource
:
private void Agenda_MouseDown(object sender, MouseButtonEventArgs e)
{
var textBlock = e.OriginalSource as TextBlock;
}
关于wpf - 通过父级上的点击事件获取子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20024143/