簡體   English   中英

如何在鼠標光標處將邊距設置到位置元素

[英]How to set margin to position element at the mouse cursor

我似乎無法弄清楚如何設置與鼠標光標所處位置相對應的邊距。

樣例代碼:

protected override void OnMouseMove(MouseEventArgs e)
{
  base.OnMouseMove(e);

  Connector connector = this.Template.FindName("PART_Connector", this) as Connector;
  double marginLeft = e.GetPosition(this).X - (connector.Width / 2);
  double marginTop = e.GetPosition(this).Y - (connector.Height / 2);

  connector.Margin = new Thickness(marginLeft, marginTop, 0, 0);
}

使用邊距定位的連接器始終偏離其應有的位置。

有什么好的建議嗎?


如果我這樣設置邊距,它將起作用,但是連接器會減小到單個點。 我希望連接器為12x12

marginLeft = e.GetPosition(this).X;
marginTop = e.GetPosition(this).Y;
marginRight = this.DesiredSize.Width - e.GetPosition(this).X;
marginBottom = this.DesiredSize.Height - e.GetPosition(this).Y;

connector.Margin = new Thickness(marginLeft, marginTop, marginRight, marginBottom);

當我重新調整邊距以為連接器提供空間時,連接器便消失了:

marginLeft = e.GetPosition(this).X - (connector.Width / 2);
marginTop = e.GetPosition(this).Y - (connector.Height / 2);
marginRight = this.DesiredSize.Width - e.GetPosition(this).X + (connector.Width / 2);
marginBottom = this.DesiredSize.Height - e.GetPosition(this).Y + (connector.Height / 2);

在這里感謝任何幫助。

好的,我已經解決了。 對於對解決方案感興趣的任何人,需要注意的是您需要提供頁邊空白的所有四個方面。

如果僅提供左側和頂部,則連接器將向網格的右下方移動。

下面概述了用於容納連接器空間的修復程序:

marginLeft = e.GetPosition(this).X - (connector.Width / 2);
marginTop = e.GetPosition(this).Y - (connector.Height / 2);
marginRight = this.DesiredSize.Width - e.GetPosition(this).X - (connector.Width / 2);
marginBottom = this.DesiredSize.Height - e.GetPosition(this).Y - (connector.Height / 2);

注意marginRight和marginTop需要減去(而不是添加)連接器大小的一半。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM