我似乎无法找到一种非常简单的方法来实现 smth:
我有ItemsControl
一个UniformGrid
里面有一个,它按 a 排列项目DataTemplate
:
<ItemsControl ItemTemplate="{StaticResource ResourceKey=tmMapCell}"
MouseUp="wpSubMap_MouseUp" BorderThickness="1" BorderBrush="DarkGray">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="8" Rows="6" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
这是单元格模板:
<DataTemplate x:Key="tmMapCell">
<Border BorderBrush="DimGray" BorderThickness="1" Margin="1" Background="Gainsboro" Visibility="{Binding Path=vMapCell}" >
<StackPanel VerticalAlignment="Center">
<TextBlock Margin="2,4,2,2" TextAlignment="Center" Text="{Binding Path=sCell0}" FontWeight="Bold" />
<StackPanel>
<StackPanel.Background><SolidColorBrush Color="{Binding Path=cColorB}" /></StackPanel.Background>
<TextBlock Margin="2,1,2,0" TextAlignment="Center" Text="{Binding Path=sCell1}" FontWeight="{Binding Path=fntWt}">
<TextBlock.Foreground><SolidColorBrush Color="{Binding Path=cColorF}" /></TextBlock.Foreground>
</TextBlock>
<TextBlock Margin="2,1,2,0" TextAlignment="Center" Text="{Binding Path=sCell2}" FontWeight="{Binding Path=fntWt}">
<TextBlock.Foreground><SolidColorBrush Color="{Binding Path=cColorF}" /></TextBlock.Foreground>
</TextBlock>
</StackPanel>
<TextBlock Margin="2" TextAlignment="Center" FontWeight="Normal" Text="{Binding Path=tElapsed, StringFormat='HH:mm:ss'}"></TextBlock>
<StackPanel Margin="0" *MouseUp*="Svc_MouseUp" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom" Background="White" Visibility="{Binding Path=vSvcs}">
<Border BorderBrush="DimGray" BorderThickness="1" Margin="2,2,0,2">
<StackPanel Name="spSvcGrn" *MouseUp*="Svc_MouseUp">
<TextBlock Name="txSvcGrn" Height="14" Width="16" TextAlignment="Center" FontWeight="Normal" Text="{Binding Path=sSvc1}" Background="Lime" Visibility="{Binding Path=vSvc1}" />
</StackPanel>
</Border>
<Border BorderBrush="DimGray" BorderThickness="1" Margin="2,2,0,2">
<StackPanel Name="spSvcOra" *MouseUp*="Svc_MouseUp">
<TextBlock Name="txSvcOra" Height="14" Width="16" TextAlignment="Center" FontWeight="Normal" Text="{Binding Path=sSvc2}" Background="Orange" Visibility="{Binding Path=vSvc2}" />
</StackPanel>
</Border>
<Border BorderBrush="DimGray" BorderThickness="1" Margin="2,2,2,2">
<StackPanel Name="spSvcYel" *MouseUp*="Svc_MouseUp">
<TextBlock Name="txSvcYel" Height="14" Width="16" TextAlignment="Center" FontWeight="Normal" Text="{Binding Path=sSvc3}" Background="Yellow" Visibility="{Binding Path=vSvc3}" />
</StackPanel>
</Border>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
这导致了一个很好的布局,根据信息填充了来自数据库的单元格(显示在左上角):
我需要捕获每个单元格内的小方块的点击(以 G、O、Y 突出显示)。我已经有一个用于整个ItemsControl
( wpSubMap_MouseUp
) 的 MouseUp 处理程序,它处理对单元格本身的点击并正常工作。我正在尝试向单个单元格项目添加新的处理程序,这就是一切停止工作的地方:
如果我只离开Svc_MouseUp
- 围绕所有正方形的外部StackPanel
,它每次都会触发;但问题是检测,实际点击了哪个方块。
如果我启用Svc_MouseUp
- 对于内部StackPanels
,包裹单个方块,它们根本不会触发!?
PS 两个 MouseUp 处理程序都使用以下模式来防止重新进入:
private void Svc_MouseUp( object sender, MouseButtonEventArgs e )
{
if( bMouseUp ) return;
Cursor cur= this.Cursor;
try
{
this.Cursor= Cursors.Wait;
bMouseUp= true;
.. /// do smth useful
}
finally
{
e.Handled= true;
bMouseUp= false;
this.Cursor= cur;
}
}
我在这里想念什么?是什么让处理程序的触发行为对 inner 如此不同StackPanels
?
我应该如何有效地为 inner 实现命中检测StackPanels
?