wpf如何以编程方式更改画布图像位置

时间:2015-12-18 00:55:10

标签: wpf

enter image description here

我是学生,在我的最后一年,我正在做一个关于WPF的项目,但我是WPF的新手,我已经分配了一些任务,但是我目前仍然坚持如何在画布上改变图像的位置。从左到右。下面是代码

%template(QuadKeyVector) vector<::QuadKey::QuadKey>;

Xaml.cs

<<Window x:Class="changing_bird_position.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="450" Width="800">
<StackPanel>
    <Canvas Name="canvas" Background="LightBlue" Width="500" Height="280" Margin="264,0,28,0">
        <Image Source="inlandbird.png" Name="Image" Height="43" Canvas.Left="55" Canvas.Top="22"/>
    </Canvas>

    <Button Content="Button" Click="Button_Click" Width="50" Height="50" Margin="246,0"/>
</StackPanel>

1 个答案:

答案 0 :(得分:0)

在你的代码背后,你不需要创建这个图像实例(img)。您可以通过它的名称属性访问该图像。

我在xaml中重命名了图像以使其清晰。

XAML:

<Window x:Class="WpfApplication15.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <Canvas Name="canvas" Background="LightBlue" Width="500" Height="280" Margin="264,0,28,0">
            <Image Source="inlandbird.png" Name="BirdImage" Height="43" Canvas.Left="55" Canvas.Top="22"/>
        </Canvas>

        <Button Content="Button" Click="Button_Click" Width="50" Height="50" Margin="246,0"/>
    </StackPanel>
</Window>

代码背后:

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Canvas.SetLeft(BirdImage, 200.0);
            Canvas.SetTop(BirdImage, 200.0);
        }
    }
}