Solved

Change the location of control at runtime in WPF application.

Posted on 2008-06-17
4
5,118 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now