6 Mart 2015 Cuma

SilverLight 4.0 ile sürükle bırak kodları (en basit haliyle)

  • önce bir silverlight projesi oluşturun. (Blend veya visual studio)
  • oluşan projedeki MainPage.xaml dosyasının içeriği.
  • LayoutRoot Canvas olacak. Sayfaya bir adet border1 ve içine bir resim ekledim. bir dikdörtgende olabilirdi.
<UserControl x:Class="suruklebirak.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="White">
<Border x:Name="border1" BorderBrush="Black" BorderThickness="1" Height="116" Canvas.Left="180" Canvas.Top="84" Width="129" MouseLeftButtonDown="border1_MouseLeftButtonDown" MouseLeftButtonUp="border1_MouseLeftButtonUp" MouseMove="border1_MouseMove" >
<Image x:Name="resim" Source="/basket.png" Stretch="Uniform"></Image>
</Border>
</Canvas>
</UserControl>
  • MainPage.xaml.cs dosyasının  içeriği.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace suruklebirak
{
public partial class MainPage : UserControl
{
private Point koordinatal;
private Boolean tasimadurum = false;
public MainPage()
{
InitializeComponent();
koordinatal = new Point();//nesne tanımla
}
private void border1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
tasimadurum = true;//taşıma aç
koordinatal = e.GetPosition(border1); //cerçeve koordinatlarını al
}
private void border1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
tasimadurum = false;//taşıma dur
}
private void border1_MouseMove(object sender, MouseEventArgs e)
{
if (tasimadurum)
{
Point p = e.GetPosition(null);//mousenin sahne üzerindeki koordinatlarını al
double x = p.X - koordinatal.X;
double y = p.Y - koordinatal.Y;
this.border1.SetValue(Canvas.LeftProperty, x);
this.border1.SetValue(Canvas.TopProperty, y);
}
}
}
}

Hiç yorum yok :

Yorum Gönder