问题
I cannot seem to find a way to achieve smth very simple:
I have an ItemsControl
with a UniformGrid
inside of it, that arranges items by 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>
Here's the cell template:
<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>
This results in a nice layout, filled with cells according to info, coming from a DB (top left corner shown):
I need to trap clicks on the little squares within each cell (highlighted in G,O,Y). I already have a MouseUp handler for entire ItemsControl
(wpSubMap_MouseUp
), which handles clicks on the cells themselves and works properly. I'm trying to add new handlers to individual cell items, and that's where everything stops working:
If i leave only Svc_MouseUp
- for the outer StackPanel
, surrounding all squares, it fires each time; but the problem is detecting, which square was actually clicked on.
If i enable Svc_MouseUp
- for inner StackPanels
, wrapping individual squares, they don't fire at all!?
P.S. Both MouseUp handlers use the following pattern to prevent re-entrance:
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;
}
}
What am i missing here? What makes handler firing behavior so different for inner StackPanels
?
And how should i efficiently implement hit detection for inner StackPanels
?
来源:https://stackoverflow.com/questions/18729145/mouseup-not-firing-on-child-stackpanels