在WPF中更改形状的位置 [英] Changing the location of shapes in WPF

查看:72
本文介绍了在WPF中更改形状的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



任何人都可以指导我如何在WPF中在运行时更改Shape的位置.

例如:我将在运行时用鼠标在画布上绘制不同的形状(矩形,椭圆形等).我能够计算形状的中心点,例如:point1(50,50).
当我单击按钮时,现在我想将相同形状移动到其他位置,例如:point2(70,60)是中心点.
现在,当我单击按钮时,我希望形状应该以相同的高度和宽度从point1移到point2.

任何人都可以为此举一个例子.

谢谢,

Hi,

Can any one Guide me how to change the positon of Shapes at run time in WPF.

Eg: I will draw different shapes(rectangle, ellipse etc) with mouse on canvas at runtime. i am able to calculate the centre point of shape, eg:point1( 50,50).
when i click on button, now i want to move the same shape to other location eg: point2(70,60) which is centre point.
now when i click on button, i want the shape should move from point1 to point2 with same height and width.

can any one post a example for this.

THanks,

推荐答案

好.这很容易.我给你举个例子.在此示例中,您将看到我使用了标签和按钮.当我们单击按钮时,标签将增大/缩小.但是我已经从后面的代码中维护了它

xaml在下面给出

Well. This is pretty easy. I am giving you a example. In this example you will see that I have used label and a button. When we click the button the label will Grow/Shrink. But i have maintain it from code behind

The xaml is given below

<Window x:Class="WpfTest.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 Height="311" Width="474">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="188,263,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="button1_Click" />
        <Label Content="Hi!!" Height="100" HorizontalAlignment="Center" Margin="188,76,186,0" Name="label1" VerticalAlignment="Top" Width="100" Background="#FFBBF23E"  />
    </Grid>
</Window>



后面的代码在下面给出



And the code behind is given below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace WpfTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            Grid grd = btn.Parent as Grid;
 
            Label lbl = grd.Children[1] as Label;
 
            if (lbl.Width < 100)
            {
                lbl.Width = 100;
            }
            else
            {
                lbl.Width = 50;
            }
 
            if (lbl.Height < 100)
            {
                lbl.Height = 100;
            }
            else
            {
                lbl.Height = 50;
            }
        }
    }
}



因此,我在这里所做的是,我只获取了按钮对象,然后是网格的按钮的父对象.在此网格中,第二个孩子是我的标签,一旦有了标签对象,就可以像在xaml中一样更改其属性.



So What i am doing here is that I just get the Button Object and then the Parent of the Button Which is Grid. In this Grid the Second Child is My Label and Once we have the Label Object we could change its Property as we could do in the xaml.


这里是一个快速且相关的示例: http://www.85turns.com/2007/12/18/animate-multiple-transforms-from-c-wpf/ [
Here''s a quick and relevant sample: http://www.85turns.com/2007/12/18/animate-multiple-transforms-from-c-wpf/[^]

Cheers.


这个 [
This[^] is a good link as well.


这篇关于在WPF中更改形状的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆