将值绑定到 XAML 中控件的绝对位置

发布于 2024-08-14 21:24:23 字数 570 浏览 13 评论 0原文

有没有办法使用 XAML 将值绑定到控件的绝对位置?

我有一个 Line< /a> 我想在两个 我的应用程序中的按钮。我认为将 Line 的起点绑定到 Button 的位置将是实现此目的的最简单方法,使用 RelativeSource 不知何故。

Is there a way to bind a value to the absolute position of a control using XAML?

I have a Line element that I would like to be drawn between two Buttons in my application. I was thinking that binding the starting point of the Line to the position of the Button would be the easiest way to make this happen, using RelativeSource somehow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第几種人 2024-08-21 21:24:23

看来你想要这样的东西:

<UserControl x:Class="PracticeSample.MyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Button x:Name="button" Content="Add" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    <Line Stroke="Black" X1="0" Y1="0" HorizontalAlignment="Center" X2="{Binding ElementName=button, Path=ActualWidth}" Y2="{Binding ElementName=button, Path=ActualHeight}"/>
</Grid>

在页面中使用此 MyButton 代替 Button,

编辑:
如果你想在两个控件之间画线
不要使用上面的代码示例,而是直接在您的页面中尝试:

<Canvas HorizontalAlignment="Left" Margin="10">
    <Button x:Name="button2" Content="Add" Canvas.Left="10" Canvas.Top="5"/>
    <Button Name="button" Content="Refresh Control" Canvas.Left="100" Canvas.Top="50"/>
    <Line Stroke="Black" X1="{Binding Path=(Canvas.Left),ElementName=button2}" Y1="{Binding Path=(Canvas.Top), ElementName=button2}" X2="{Binding (Canvas.Left), ElementName=button}" Y2="{Binding (Canvas.Top), ElementName=button}"/>
</Canvas>

希望这有帮助!

It seems you want something like this:

<UserControl x:Class="PracticeSample.MyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Button x:Name="button" Content="Add" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    <Line Stroke="Black" X1="0" Y1="0" HorizontalAlignment="Center" X2="{Binding ElementName=button, Path=ActualWidth}" Y2="{Binding ElementName=button, Path=ActualHeight}"/>
</Grid>

use this MyButton in your pages in place of Button,

Edit:
if you want to draw line between two controls
don't use above code sample but try this directly in your page:

<Canvas HorizontalAlignment="Left" Margin="10">
    <Button x:Name="button2" Content="Add" Canvas.Left="10" Canvas.Top="5"/>
    <Button Name="button" Content="Refresh Control" Canvas.Left="100" Canvas.Top="50"/>
    <Line Stroke="Black" X1="{Binding Path=(Canvas.Left),ElementName=button2}" Y1="{Binding Path=(Canvas.Top), ElementName=button2}" X2="{Binding (Canvas.Left), ElementName=button}" Y2="{Binding (Canvas.Top), ElementName=button}"/>
</Canvas>

Hope this helps!

醉态萌生 2024-08-21 21:24:23

定义一个模板,将按钮和线条放置在您喜欢的正确位置,然后在按钮的位置使用此模板。

Define a Template with Button and Line positioned at right places wherever you like and then use this Template at the place of Button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文