我有一个简单的房子图,我想调整屋顶改变它的宽度,因为房屋的主体改变宽度和/或门适应房屋的宽度。我已经在任何解决方案上工作了几个小时,但是有效的代码似乎只是复杂地实现这样的初学者项目。这是我的代码。我已尝试使用一些函数将body连接到mouseLeftButtonDown但它无效。
<Window x:Class="LAB2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="600">
<Canvas UseLayoutRounding="True">
<Rectangle Canvas.Left="86" Canvas.Top="190" Height="171" Fill="Blue" Name="body" Stroke="Black" MouseEnter="body_MouseEnter" MouseLeave="body_MouseLeave" Width="395" />
<Rectangle Canvas.Left="118" Canvas.Top="229" Height="82" Fill="{Binding ElementName=body, Path=Fill}" Name="window" Stroke="Black" Width="89"/>
<Rectangle Canvas.Left="346" Canvas.Top="229" Fill="{Binding ElementName=body, Path=Fill}" Height="132" Name="door" Stroke="Black" Width="83"/>
<Polygon Points="10,110 230,10 500,110" Fill="{Binding ElementName=body, Path=Fill}" Stroke="Black" Name="triangle" Canvas.Left="35" Canvas.Top="86" />
<Rectangle Canvas.Left="156" Canvas.Top="109" Height="61" Fill="{Binding ElementName=body, Path=Fill}" Name="chimney" Stroke="Black" Width="36" />
<Button Canvas.Left="491" Canvas.Top="12" Content="Click" Height="23" Name="button1" Width="75" />
</Canvas>
</Window>
我将非常感谢有关此任务的任何建议或指导。
答案 0 :(得分:1)
好的,这是你的解决方案......我再次使用了简单的触发器。
<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>
唯一的变化就是我用Path替换了多边形。希望我帮忙。您可以使用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>
和CodeBehind:
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;
}