Click here to Skip to main content
15,884,058 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

Can any one Guide me how to change the positon of Shapes at run time in WPF.

Eg: I will draw different shapes(rectangle, ellipse etc) with mouse on canvas at runtime. i am able to calculate the centre point of shape, eg:point1( 50,50).
when i click on button, now i want to move the same shape to other location eg: point2(70,60) which is centre point.
now when i click on button, i want the shape should move from point1 to point2 with same height and width.

can any one post a example for this.

THanks,
Posted

Well. This is pretty easy. I am giving you a example. In this example you will see that I have used label and a button. When we click the button the label will Grow/Shrink. But i have maintain it from code behind

The xaml is given below

XML
<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Height="311" Width="474">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="188,263,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="button1_Click" />
        <Label Content="Hi!!" Height="100" HorizontalAlignment="Center" Margin="188,76,186,0" Name="label1" VerticalAlignment="Top" Width="100" Background="#FFBBF23E"  />
    </Grid>
</Window>


And the code behind is given below

C#
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;
 
namespace WpfTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            Grid grd = btn.Parent as Grid;
 
            Label lbl = grd.Children[1] as Label;
 
            if (lbl.Width < 100)
            {
                lbl.Width = 100;
            }
            else
            {
                lbl.Width = 50;
            }
 
            if (lbl.Height < 100)
            {
                lbl.Height = 100;
            }
            else
            {
                lbl.Height = 50;
            }
        }
    }
}


So What i am doing here is that I just get the Button Object and then the Parent of the Button Which is Grid. In this Grid the Second Child is My Label and Once we have the Label Object we could change its Property as we could do in the xaml.
 
Share this answer
 
Comments
Md. Rashim Uddin 25-Aug-11 16:35pm    
You give me the vote..Please mark it as answer so that others will get help from it.Please please MARK it as a Answer
Md. Rashim Uddin 26-Aug-11 7:28am    
Is it Solve Your Problems??
KiranBabu M 26-Aug-11 8:13am    
Hi Rashim,

Your solution is not exactly want i wanted, but helped me to solve the problem.
But this is a different question u have posted the same answer of my previous question.i have already commented on ur answer over there.

Thanks,
Md. Rashim Uddin 26-Aug-11 8:49am    
I guess you are in the same prob. Thanks
 
Share this answer
 
This[^] is a good link as well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900