MouseUp没有在子StackPanels上触发

时间:2013-09-10 21:37:37

标签: wpf-4.0

我似乎无法找到实现smth的方法非常简单:

我的ItemsControl内有UniformGrid,用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>

这会产生一个漂亮的布局,根据信息填充单元格,来自DB(显示的左上角):

Top left corner of the grid

我需要在每个单元格中的小方块上捕获点击(在G,O,Y中突出显示)。我已经有一个整个ItemsControlwpSubMap_MouseUp)的MouseUp处理程序,它处理单元格本身的点击并正常工作。我正在尝试为单个单元格项添加新的处理程序,这就是一切都停止工作的地方:

如果我只留下Svc_MouseUp - 对于外围StackPanel,围绕所有正方形,每次都会触发;但问题是检测到,实际点击了哪个方格。

如果我启用Svc_MouseUp - 对于内部StackPanels,包裹个别方格,它们根本不会开火!?

P.S。两个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;
    }
}

我在这里缺少什么?内部StackPanels处理程序触发行为的不同之处是什么? 我应该如何有效地实现内部StackPanels的命中检测?

0 个答案:

没有答案