WPF中的用户控制位置 - PointToScreen问题 [英] User Control position in WPF- Problems with PointToScreen

查看:780
本文介绍了WPF中的用户控制位置 - PointToScreen问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个WPF应用程序,我也在使用图书馆Modern UI。我试图在网格/屏幕的相对位置上设置我的用户控件。我的最终目标是将我的用户控件(在形状或背景图像上)放在我定义的x / y位置。







我一直在阅读这个主题,他们建议使用:



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:

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:

<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>



在Home页面后面的代码中我添加了:




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

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);

        }
    }



它返回一个解析错误:'



附加信息:'匹配指定绑定约束的类型Test.Pages.Home上的构造函数的调用'引发了异常。'行号'8'和行位置'6'。



我尝试使用注释行添加代码后面的代码(而不是XAML),但它也没有用。



为什么会这样?有关更好地实现目标的任何建议吗?


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?

推荐答案

public Home()
        { 
            InitializeComponent();
            Loaded += (s,e)=>DoStuff();
        }
private DoStuff()
        {
            Point locationFromWindow = b1.TranslatePoint(new Point(0, 0),this );
            Point locationFromScreen = b1.PointToScreen(locationFromWindow);
        }


这篇关于WPF中的用户控制位置 - PointToScreen问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆