无法在vs [C#,WPF]中将运行时创建的控件位置正确设置为光标位置 [英] Can't set runtime created controls position to cursor position correctly in vs [C#, WPF]
问题描述
我完成了一个简单的拖动控制功能,后来发现它只适用于设计/创建的控件,但不适用于运行时控件。当我单击/保持运行时控件时,其位置未正确设置到我的光标位置,它设置在距离光标稍远的右下方,但创建的控件工作正常。我想我将运行时控件添加到我的网格错误。这是我的xml,我将我的运行时控件添加到displaygrid。
I finished creating a simple drag control function and later found out that it only works on designed/created controls, but not runtime controls. When I click/hold a runtime control its position is not set correctly to my cursor position, it is set to right bottom a little far away from my cursor, but created controls works fine. I think I'm adding the runtime controls to my grid wrong. Here is my xml, I'm adding my runtime controls to the displaygrid.
<Window x:Class="MN.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MN"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen" Topmost="True" ResizeMode="NoResize" Background="{x:Null}" AllowsTransparency="True" WindowStyle="None">
<Grid x:Name="displaygrid">
<Rectangle Fill="#FFF4F4F5" Stroke="Black">
<Rectangle.Effect>
<BlurEffect Radius="25"/>
</Rectangle.Effect>
</Rectangle>
<ListBox x:Name="tablist" Background="{x:Null}" FontSize="48" SelectionChanged="tablist_SelectionChanged"/>
</Grid>
</Window>
我添加了这样的运行时控件
I added my runtime control like this
TextBlock newctrl = new TextBlock();
displaygrid.Children.Add(newctrl);
newctrl.Width = ctrl.width;
newctrl.Height = ctrl.height;
newctrl.Margin = new Thickness(ctrl.left, ctrl.top, 0, 0);
newctrl.Text = ctrl.text;
active_ctrls.Add(newctrl);
newctrl.PreviewMouseDown += enable_ctrldrag;
这样做是为了将我的控制移动到我的光标
And did this to move my control to my cursor
private bool MouseHook_OnMouseMove(int x, int y) // WinputManager Library
{
if (_selctrl != null)
{
//_selctrl.Margin = new Thickness(x, y, 0, 0);
_selctrl.Margin = new Thickness(Mouse.GetPosition(this).X, Mouse.GetPosition(this).Y, 0, 0);
}
return false;
}
我的尝试:
制作一个新项目并测试并发现我的问题+我在测试时没有使用任何库。
What I have tried:
Made a new project and tested and found out my problem + I didn't use any libraries when I was testing.
推荐答案
找到我的答案当我在WPF中搞乱时,通过添加VerticalAlignment = Top和HorizontalAlignment = Left来解决它
Found my answer when I was messing around in WPF, solved it by adding VerticalAlignment = Top and HorizontalAlignment = Left
TextBlock newctrl = new TextBlock();
displaygrid.Children.Add(newctrl);
newctrl.VerticalAlignment = VerticalAlignment.Top;
newctrl.HorizontalAlignment = HorizontalAlignment.Left;
newctrl.Width = ctrl.width;
newctrl.Height = ctrl.height;
newctrl.Margin = new Thickness(ctrl.left, ctrl.top, 0, 0);
newctrl.Text = ctrl.text;
active_ctrls.Add(newctrl);
newctrl.PreviewMouseDown += enable_ctrldrag;
这篇关于无法在vs [C#,WPF]中将运行时创建的控件位置正确设置为光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!