简体   繁体   English

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

[英]Not able to rotate the grid in usercontrol from the centre of grid in wpf

I have one main page and one user control and have one button in User control with events for rotating.Events are firing for rotating but not from the centre but from corner.But I want rotate the grid withing the centre.So the user can easily rotate the see from different angle.I was trying to build one application for building map.so the user can rotate the floor to see from different angle ..Please help how to fix this issue My User Control Xamal : 我有一个主页和一个用户控件,并在用户控件中有一个按钮,它带有旋转事件。事件是旋转触发的,但不是从中心而是从角落开始的,但是我想在中心旋转网格,因此用户可以轻松从不同角度旋转视角。我正在尝试构建一个用于构建地图的应用程序。因此用户可以旋转地板从不同角度观看。.请帮助如何解决此问题我的用户控件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();

        }

    }

}

And my Main page xamal: 我的主页xamal:

<Window x:Class="Floorsreen.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:view="clr-namespace:Floorsreen" 
        Title="MainWindow" Height="800" Width="1000">

    <Window.Resources>

    </Window.Resources>

    <Grid>


        <view:FloorUserControl />


    </Grid>
</Window>

You need to specify the RenderTransformOrigin . 您需要指定RenderTransformOrigin This property takes two doubles, usually from 0 - 1, which specify X and Y pivot location in percent. 此属性需要两个双精度值,通常从0到1,以百分比指定X和Y枢轴位置。 Meaning RenderTransformOrigin="0.5, 0.5" will start in the center where the default RenderTransformOrigin="0, 0" is the upper left corner. 含义RenderTransformOrigin="0.5, 0.5"将从中心开始,默认RenderTransformOrigin="0, 0"是左上角。 RenderTransformOrigin="1, 1" is bottom right and so on. RenderTransformOrigin="1, 1"在右下角,依此类推。

To rotate in the center you need to add this attachable property to your grid. 要在中心旋转,您需要将此可附加属性添加到网格中。 Example: 例:

<Grid RenderTransformOrigin="0.5, 0.5">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM