有没有办法让我可以点击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>
答案 0 :(得分:4)
您可以在MouseDown
上处理WrapPanel
事件,因为它会从该面板中的任何控件冒出来
<WrapPanel Name="Agenda" ... MouseDown="Agenda_MouseDown">
并且在事件处理程序中,您可以像这样检查OriginalSource
:
private void Agenda_MouseDown(object sender, MouseButtonEventArgs e)
{
var textBlock = e.OriginalSource as TextBlock;
}