Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,
I have a WPF app and I am using the library Modern UI too. I am trying to situate on a Page my user control in a relative position of the grid/screen. My final objective is to position my user controls (over a shape or background image) in x/y positions defined by me.



I have been reading about this topic and they recomend to use:

C#
Point locationFromWindow = myusercontrol.TranslatePoint(new Point(0, 0),this );
Point locationFromScreen = myusercontrol.PointToScreen(locationFromWindow);

It looks simple but is giving me next error:

For now I am testing it with a simple button in my page:

XML
<Grid Style="{StaticResource ContentRoot}">
      <Grid ShowGridLines="True" x:Name="mainGrid">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
          <Button   Content="Button" x:Name="b1"                            />

    </Grid>
    </Grid>

In the code behind of the page "Home" I added:

C#
public partial class Home : UserControl
    {
        public Home()
        { 
            InitializeComponent();
            //Button b1 = new Button();
            Point locationFromWindow = b1.TranslatePoint(new Point(0, 0),this );
            Point locationFromScreen = b1.PointToScreen(locationFromWindow);
           // mainLayout.Children.Add(b1);

        }
    }

It returns a parsing error: '

Additional information: 'The invocation of the constructor on type Test.Pages.Home' that matches the specified binding constraints threw an exception.' Line number '8' and line position '6'.

I tried adding the button from the code behind too ( and not from the XAML) with the commented lines , but it also didn't works.

Why is this happening? Any suggestion for a better performance of my objective?
Posted
Updated 14-Jan-15 4:57am
v2
Comments
mudit_malhotra 30-Sep-14 10:37am    
can you share the inner exception ?

1 solution

C#
public Home()
        { 
            InitializeComponent();
            Loaded += (s,e)=>DoStuff();
        }
private DoStuff()
        {
            Point locationFromWindow = b1.TranslatePoint(new Point(0, 0),this );
            Point locationFromScreen = b1.PointToScreen(locationFromWindow);
        }
 
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