如何绑定网格

时间:2016-03-14 10:31:33

标签: c# wpf data-binding binding coordinates

我在网格上有一个图像(名为car1),我想将car1坐标绑定到textblock

<Canvas>
    <Image Name="car1" />
</Canvas>

<WrapPanel>
    <TextBlock Text="" Margin="10" Name="xx" Width="50"></TextBlock>
    <TextBlock Text="" Margin="10" Name="yy" Width="50">0</TextBlock>
</WrapPanel>

如何解决?

1 个答案:

答案 0 :(得分:1)

试试这个

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Canvas>
            <Image Name="car1" Canvas.Left="40" Canvas.Top="30"/>
        </Canvas>

        <WrapPanel>
            <TextBlock Text="{Binding Path=(Canvas.Left), ElementName=car1}" Margin="10" Name="Left" Width="50"/>
            <TextBlock Text="{Binding Path=(Canvas.Top), ElementName=car1}" Margin="10" Name="Top" Width="50"/>
        </WrapPanel>

    </Grid>
</Window>