Solved

Change the location of control at runtime in WPF application.

Posted on 2008-06-17
4
5,181 Views
1 Endorsement
Last Modified: 2013-12-16
How to change the location (left and top) of the image control in wpf windows application at runtime?
1
Comment
Question by:Mr_Javed
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
BitRunner303 earned 50 total points
ID: 21810607
Not very intuitively...  here I've got a form with a button and a textbox both sitting inside a Canvas layout object, I'll use the Click event of the button to change the location of the textbox.  Basically this works by getting the existing X/Y of the textbox, then I'll offset it by a +/- amouhnt in transX/Y.  The values I'm setting are the "attached properties" for it that come from it being inside a Canvas container.

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int transX = 50;
            int transY = 20;

            GeneralTransform myTrans = this.textBox1.TransformToAncestor(this);
            Point p1 = myTrans.Transform(new Point(0, 0));

            this.textBox1.SetValue(Canvas.LeftProperty, p1.X + transX);
            this.textBox1.SetValue(Canvas.TopProperty, p1.Y + transY);

        }
1
 

Author Comment

by:Mr_Javed
ID: 21810678
Thanks for the reply.

When I created the sample like yours. in VB

==============================================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        TextBox1.SetValue(Canvas.LeftProperty, 10)
    End Sub
==============================================================

This give me the error : '10' is not a valid value for property 'Left'.
When I changed the value 10 to any other, the same error I'm getting.

What can be the problem?




0
 

Author Comment

by:Mr_Javed
ID: 21811662
That error is solved now. that was because I pass 10, that is integer value and it requires double value.

your solution works. Thanks BitRunner303

0
 

Author Closing Comment

by:Mr_Javed
ID: 31468228
Thanks a lot
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question