开发者

canvas not occupying full space in grid

开发者 https://www.devze.com 2023-02-14 03:12 出处:网络
I have a canvas in grid. On the mousemove event of canvas i am trying to translate the canvas. It works fine. However my canvas doesn\'t occupy the full space in the grid. What changes do i need to ma

I have a canvas in grid. On the mousemove event of canvas i am trying to translate the canvas. It works fine. However my canvas doesn't occupy the full space in the grid. What changes do i need to make in this program so that the canvas occupies the full space.

This is my xaml :-

<UserControl x:Class="SilverlightTestCanvasDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

            <Canvas x:Name="canvas" Margin="0" Background="Red">
                <TextBlock Canvas.Left="84" TextWrapping="Wrap" Text="This is some text to test whether screen is moving" Canvas.Top="99" Height="84" Width="196" FontSize="18.667" FontFamily="Cambria"/>
            </Canvas>

    </Grid>
</UserControl>

This is the code :-

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SilverlightTestCanvasDemo
{
    public partial class MainPage : UserControl
    {
        private bool _isDown = false;
        private Point _lastPoint;

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            canvas.MouseLeftButtonDown += canvas_MouseLeftButtonDown;
            canvas.MouseLeftButtonUp += canvas_MouseLeftButtonUp;
            canvas.MouseMove += canvas_MouseMove;
        }

        void canvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (_isDown)
            {
                Point point = e.GetPosition(canvas);
                double deltaX = point.X - _lastPoint.X;
                double deltaY = point.Y - _lastPoint.Y;
                CompositeTransform transform = null;
                if (!(canvas.RenderTransform is CompositeTransform))
                {
                    transform = new CompositeTransform();
                    canvas.RenderTransform = transform;
                }
                else
                {
                    transform = canvas.RenderTransform as CompositeTransform;
                }

                transform.TranslateX += deltaX;
                transform.TranslateY += deltaY;

                //canvas.Height += deltaY;
                //canvas.Width += deltaX;


                _lastPoint = e.GetPosition(canvas);
            }
        }

        void canvas_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonE开发者_开发百科ventArgs e)
        {
            _isDown = false;
        }

        void canvas_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _isDown = true;
            _lastPoint = e.GetPosition(canvas);
        }
    }
}

Thanks in advance :)


Try this:-

<Grid x:Name="LayoutRoot">
    <Canvas x:Name="canvas" Margin="0" Background="AliceBlue">
        <Canvas>
            <TextBlock Canvas.Left="84" TextWrapping="Wrap" Text="This is some text to test whether screen is moving" Canvas.Top="99" Height="84" Width="196" FontSize="18.667" FontFamily="Cambria"/>
        </Canvas>
    </Canvas>
</Grid>

and tweak the mouse move code behind to this:-

   void canvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (_isDown)
        {
            Point point = e.GetPosition(canvas);
            double deltaX = point.X - _lastPoint.X;
            double deltaY = point.Y - _lastPoint.Y;
            CompositeTransform transform = null;

            if (!(canvas.Children[0].RenderTransform is CompositeTransform))
            {
                transform = new CompositeTransform();
                canvas.Children[0].RenderTransform = transform;
            }
            else
            {
                transform = canvas.Children[0].RenderTransform as CompositeTransform;
            }

            transform.TranslateX += deltaX;
            transform.TranslateY += deltaY;

            _lastPoint = e.GetPosition(canvas);
        }
    }

What's happening here is a second inner Canvas is being used as a caddy to hold the set of actual children. Now you need only transform this inner canvas to move all the content about.

0

精彩评论

暂无评论...
验证码 换一张
取 消