好的,这是您的解决方案...我再次使用了简单的触发器。
<Control>
<Control.Template>
<ControlTemplate>
<Canvas>
<Grid x:Name="HouseBody" Height="153" Width="350" HorizontalAlignment="Left" VerticalAlignment="Bottom" Canvas.Left="78" Canvas.Top="159">
<Rectangle Height="Auto" Fill="Blue" x:Name="body" Stroke="Black" Width="Auto"/>
<Rectangle Height="Auto" Fill="Blue" x:Name="window" Stroke="Black" Width="89" HorizontalAlignment="Left" Margin="25,17,0,54"/>
<Rectangle Fill="Blue" Height="Auto" x:Name="door" Stroke="Black" Width="Auto" Margin="235.5,17,25.5,0" HorizontalAlignment="Stretch"/>
</Grid>
<Grid x:Name="HouseRoof" Height="131.5" Width="350" HorizontalAlignment="Left" VerticalAlignment="Bottom" Canvas.Left="77" Canvas.Top="27.5">
<Rectangle Height="Auto" Fill="Blue" x:Name="chimney" Stroke="Black" Width="40" HorizontalAlignment="Stretch" Margin="44.066,22.965,265.934,8" />
<Path x:Name="path" Data="M74.752528,159.37536 L429.11068,159.37578 242,26.5 z" Fill="Blue" Height="Auto" Stretch="Fill" Stroke="Black" Width="Auto" Margin="0.25,0,-2.589,-0.865"/>
</Grid>
</Canvas>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="body" Property="Fill" Value="Red" />
<Setter TargetName="window" Property="Fill" Value="Red" />
<Setter TargetName="door" Property="Fill" Value="Red" />
<Setter Property="Fill" TargetName="path" Value="Red"/>
<Setter Property="Fill" TargetName="chimney" Value="Red"/>
<Setter Property="Width" TargetName="HouseBody" Value="395"/>
<Setter Property="Width" TargetName="HouseRoof" Value="395"/>
</Trigger>
<!--<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="body" Property="Fill" Value="Green" />
<Setter TargetName="window" Property="Fill" Value="Green" />
<Setter TargetName="door" Property="Fill" Value="Green" />
<Setter TargetName="triangle" Property="Fill" Value="Green" />
</Trigger>-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Control.Template>
</Control>
唯一的大变化是我用路径替换了多边形。希望我有所帮助。您可以使用 StoryBoards 执行相同的操作,当然,当您将对象分组到命名网格中时,使用 codeBehind 和 C# 会容易得多。您只需设置网格宽度并完成。
它会是这样的:
XAML:
<Grid>
<Canvas MouseLeftButtonDown="Canvas_MouseLeftButtonDown" MouseLeftButtonUp="Canvas_MouseLeftButtonUp">
<Grid x:Name="HouseBody" Height="153" Width="350" HorizontalAlignment="Left" VerticalAlignment="Bottom" Canvas.Left="78" Canvas.Top="159">
<Rectangle Height="Auto" Fill="Blue" x:Name="body" Stroke="Black" Width="Auto"/>
<Rectangle Height="Auto" Fill="Blue" x:Name="window" Stroke="Black" Width="89" HorizontalAlignment="Left" Margin="25,17,0,54"/>
<Rectangle Fill="Blue" Height="Auto" x:Name="door" Stroke="Black" Width="Auto" Margin="235.5,17,25.5,0" HorizontalAlignment="Stretch"/>
</Grid>
<Grid x:Name="HouseRoof" Height="131.5" Width="350" HorizontalAlignment="Left" VerticalAlignment="Bottom" Canvas.Left="77" Canvas.Top="27.5">
<Rectangle Height="Auto" Fill="Blue" x:Name="chimney" Stroke="Black" Width="40" HorizontalAlignment="Stretch" Margin="44.066,22.965,265.934,8" />
<Path x:Name="path" Data="M74.752528,159.37536 L429.11068,159.37578 242,26.5 z" Fill="Blue" Height="Auto" Stretch="Fill" Stroke="Black" Width="Auto" Margin="0.25,0,-2.589,-0.865"/>
</Grid>
</Canvas>
</Grid>
和代码隐藏:
private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
HouseBody.Width = 400;
HouseRoof.Width = 400;
}
private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
HouseBody.Width = 350;
HouseRoof.Width = 350;
}