Solved

Change the location of control at runtime in WPF application.

Posted on 2008-06-17
4
5,225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 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