无法从WPF中的网格中心旋转用户控件中的网格

时间:2018-10-03 17:23:34

标签: c# wpf

我有一个主页和一个用户控件,并在用户控件中有一个带有旋转事件的按钮。事件是旋转的,但不是从中心而是从角落触发的,但是我想在中心位置旋转网格。用户可以轻松地从不同角度旋转视线。我正在尝试构建一个用于构建地图的应用程序。因此用户可以旋转地板以从不同角度观看视线。请帮助解决此问题 我的用户控件Xamal:

<div class="window">
  <div class="header">
    <div class="burger-container">
      <div id="burger">
        <div class="bar topBar"></div>
        <div class="bar btmBar"></div>
      </div>
    </div>
    <div class="icon icon-apple"></div>
    <ul class="menu">
      <li class="menu-item"><a href="#">Mac</a></li>
      <li class="menu-item"><a href="#">iPad</a></li>
      <li class="menu-item"><a href="#">iPhone</a></li>
      <li class="menu-item"><a href="#">Watch</a></li>
      <li class="menu-item"><a href="#">TV</a></li>
      <li class="menu-item"><a href="#">Music</a></li>
      <li class="menu-item"><a href="#">Support</a></li>
    </ul>
    <div class="shop icon icon-bag"></div>
  </div>
  <div class="content"> <img src="https://images.apple.com/v/ipad-air-2/c/images/overview/performance_large.png" alt=""/>
    <h2>Thin. Light. Epic.</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas recusandae ullam repellat, soluta, reprehenderit suscipit reiciendis minus ratione alias dolor eveniet rerum.</p>
  </div>
</div>

我的主页xamal:

    <UserControl x:Class="Floorsreen.FloorUserControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="600" d:DesignWidth="600" Background="Yellow"   >



        <Grid Width="600" Height="600" Background="Yellow" >

            <Grid Width="500" Height="500" Background="Wheat" Name="my_grid">
                <Grid.RenderTransform>
                    <RotateTransform x:Name="transform" />
                </Grid.RenderTransform>
                <Grid HorizontalAlignment="Stretch" >
            <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />

                </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                     <RowDefinition Height="*" />
                     <RowDefinition Height="20"  />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="20"  />

                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="20" />
                </Grid.RowDefinitions>



            </Grid>


        </Grid>
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="516,582,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        </Grid>
    </UserControl>

My UserControl code behind:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;


namespace Floorsreen
{
    /// <summary>
    /// Interaction logic for FloorUserControl.xaml
    /// </summary>
    public partial class FloorUserControl : UserControl

    {

        public FloorUserControl()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Storyboard storyboard = new Storyboard();
            DoubleAnimation rotateAnimation = new DoubleAnimation()
            {
                From = 0,
                To = 90,
                Duration = new Duration(TimeSpan.FromSeconds(10.0))
            };
            Storyboard.SetTarget(rotateAnimation, my_grid);
            Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));

            storyboard.Children.Add(rotateAnimation);
            storyboard.Begin();

        }

    }

}

1 个答案:

答案 0 :(得分:-1)

您需要指定RenderTransformOrigin。此属性需要两个双精度值,通常从0到1,以百分比指定X和Y枢轴位置。意思是RenderTransformOrigin="0.5, 0.5"从中间的位置开始,默认的RenderTransformOrigin="0, 0"是左上角。 RenderTransformOrigin="1, 1"位于右下角,依此类推。

要在中心旋转,您需要将此可附加属性添加到网格中。 示例:

<Grid RenderTransformOrigin="0.5, 0.5">