0

我有这个简单的房子图,我想调整屋顶改变它的宽度,因为房屋主体改变它的宽度和/或门适应房屋主体的宽度。我已经在任何解决方案上工作了几个小时,但是对于像这样的初学者项目来说,有效的代码似乎很复杂。这是我的代码。我尝试了一些将主体与 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>  

我将非常感谢有关此任务的任何建议或指导。

4

1 回答 1

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>

唯一的大变化是我用路径替换了多边形。希望我有所帮助。您可以使用 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;
    }
于 2012-11-24T14:02:54.633 回答