代码之家  ›  专栏  ›  技术社区  ›  sourcenouveau

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

  •  2
  • sourcenouveau  · 技术社区  · 16 年前

    是否有方法使用XAML将值绑定到控件的绝对位置?

    我有一个 Line 要在两个元素之间绘制的元素 Button 在我的申请中。我在想把 线 到的位置 纽扣 是实现这一目标的最简单方法,使用 RelativeSource 不知何故。

    2 回复  |  直到 12 年前
        1
  •  2
  •   viky    16 年前

    你似乎想要这样的东西:

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

    希望这有帮助!

        2
  •  0
  •   viky    16 年前

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

    推荐文章