没有在Grid上调用事件

时间:2014-03-18 17:40:47

标签: c# wpf events

有问题的代码

<Canvas x:Name="CanvaContainer" Margin="0,0,0,0" Background="Transparent" Height="360" Width="540" MouseMove="CanvaContainer_MouseMove" MouseLeftButtonDown="CanvaContainer_MouseLeftButtonDown" MouseRightButtonDown="CanvaContainer_MouseRightButtonDown">
            <Grid x:Name="GridContainer" Background="Transparent" Height="360" Width="540">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <Rectangle Grid.Column="0" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="1" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="2" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="3" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="4" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="5" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="6" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="7" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="8" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="9" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="10" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                <Rectangle Grid.Column="11" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
            </Grid>
        </Canvas>

整个代码

 <Grid Name="GraphGrid" Background="Transparent" Margin="140,30,0,0" Width="582" Height="620" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Border x:Name="CanvasBorder" BorderThickness="1" BorderBrush="Black"  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="40,230,0,0" >
            <Canvas x:Name="CanvaContainer" Margin="0,0,0,0" Background="Transparent" Height="360" Width="540" MouseMove="CanvaContainer_MouseMove" MouseLeftButtonDown="CanvaContainer_MouseLeftButtonDown" MouseRightButtonDown="CanvaContainer_MouseRightButtonDown">
                <Grid x:Name="GridContainer" Background="Transparent" Height="360" Width="540">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <Rectangle Grid.Column="0" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="1" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="2" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="3" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="4" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="5" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="6" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="7" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="8" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="9" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="10" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                    <Rectangle Grid.Column="11" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"/>
                </Grid>
            </Canvas>

        </Border>

        <Border HorizontalAlignment="Left" Height="18" Margin="40,0,0,0" VerticalAlignment="Top" Width="542" BorderBrush="Black" >
            <Grid x:Name="gridMonth">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                    <ColumnDefinition  Width="45" />
                </Grid.ColumnDefinitions>
                <Border Grid.Column="0" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock Text="{Binding sSep, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>

                <Border Grid.Column="1" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock   Text="{Binding sOct, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>

                <Border Grid.Column="2" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock  Text="{Binding sNov, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>

                <Border Grid.Column="3" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock   Text="{Binding sDec, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>
                <Border Grid.Column="4" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock  Text="{Binding sJan, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>

                <Border Grid.Column="5" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock   Text="{Binding sFeb, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>
                <Border Grid.Column="6" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock  Text="{Binding sMar, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>

                <Border Grid.Column="7" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock   Text="{Binding sApr, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>
                <Border Grid.Column="8" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock  Text="{Binding sMay, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>

                <Border Grid.Column="9" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock   Text="{Binding sJune, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>
                <Border Grid.Column="10" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock  Text="{Binding sJul, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>

                <Border Grid.Column="11" BorderBrush="Black" BorderThickness="1,1,1,0" CornerRadius="1" >
                    <TextBlock   Text="{Binding sAug, Source={StaticResource Resources}}" HorizontalAlignment="Center"/>
                </Border>
            </Grid>
        </Border>

        <Border BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Left" Height="480" Margin="0,230,0,0" VerticalAlignment="Top" Width="40">
            <Grid x:Name="depthLeader">
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                </Grid.RowDefinitions>

                <Border Grid.Row="0" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth0" Text="-0,25m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="1" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth1" Text="-0,5m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="2" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth2" Text="-0,75m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="3" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth3" Text="-1m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="4" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth4" Text="-1,5m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="5" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth5" Text="-2m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="6" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth6" Text="-2,5m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="7" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth7" Text="-3m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="8" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth8" Text="-3,5m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="9" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth9" Text="-4m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="10" BorderBrush="Black" BorderThickness="1,1,0,1" CornerRadius="3">
                    <TextBlock Name="depth10" Text="-4,5m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <Border Grid.Row="11" BorderBrush="Black" BorderThickness="1,0,0,1" CornerRadius="3">
                    <TextBlock Name="depth11" Text="-5m" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </Grid>
        </Border>

        <Button Margin="20,-10,542,590" Name="nextMonth" Content="&gt;" Background="LightGray" Width="20" Height="20" Click="nextMonth_Click"/>
        <Button  Margin="0,-10,562,590" Name="previousMonth" Content="&lt;" Background="LightGray" Width="20" Height="20" Click="previousMonth_Click"/>
        <Grid x:Name="AirTempGd" Background="Transparent" HorizontalAlignment="Left" Height="175" Margin="10,20,0,0" VerticalAlignment="Top" Width="572"/>


    </Grid>

这就是我正在编写的代码,我把它全部交给了你,这样你就可以更好地获得上下文。所以我的问题是鼠标输入事件没有被调用我,尝试使用断点来控制输出并且它永远不会被调用我不知道为什么我还尝试了其他事件,如MouseDown或MouseLeave,它甚至还没有工作背景是透明的我不知道哪里是我的问题我可以使用一些帮助。

修改

我可能错了,但我认为它是因为它在另一个容器中,但我不知道它是否是问题,如果它是我不知道如何修复它除了移除容器但我需要它们。但如果这是错误将是愚蠢的,所以我会感到惊讶。

1 个答案:

答案 0 :(得分:0)

原因是您的矩形缺少内容。简单的解决方法是用透明色填充它

<Rectangle Fill="Transparent" Grid.Column="0" Height="{Binding ElementName=CanvaContainer, Path=ActualHeight}" MouseEnter="Rectangle_MouseEnter"