开发者

Event firing problem in WPF

I have following grid:

<Grid x:Name="grd" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="100" >
    <Button x:Name="but" OverridesDefaultStyle="True" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top"  >
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
         开发者_高级运维   <Border Name="Bd" Style="{StaticResource MatrixStyle}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Height="50" />

        </ControlTemplate>
    </Button.Template>
    </Button>
    <Border BorderBrush="Black" BorderThickness="1" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    <Border BorderBrush="Black" BorderThickness="1" Width="50" Height="50" HorizontalAlignment="Right" VerticalAlignment="Top"/>
    <Border BorderBrush="Black" BorderThickness="1" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
    <Border BorderBrush="Black" BorderThickness="1" Width="50" Height="50" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>     
    </Grid>

This parent grid has MouseDown event attached to it. If I keep the 4 Borders and remove the buttons this event gets fired.But If I keep only the button and remove the other borders this event does not get fired. what may be the probable cause of this problem?


The Button handles the MouseDown event and "consumes" it (i.e. it prevents it from routing up further to its parent by setting "e.Handled = true" for the MouseDown event). Thus, it doesn't reach your Grid control.

Having said that, you might want to handle the PreviewMouseDown event instead. This one gets fired first in the Grid before it reaches the Button so you'll surely be able to handle it on the Grid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜