Reputation: 11
I am trying to develop an app which is able to drag some objects into inkcanvas. these objects must have the capability of selection, change position , mayebe resize and so on. Can any one help me on this question? Is there any way to get access to the selected inkcanvas child in run time inorder to change the position of it ? The inkcanvas children will add to inkcanvas whenever a new drag&drop events take place. ( please take a look at the below code) I already take a look at the inkcanvas's events but I couldnt find anything. I would appreciate any help. Thanks.
private void Canvas1_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetData(e.Data.GetFormats()[0]) is Image)
{
Image im = (Image)e.Data.GetData(e.Data.GetFormats()[0]);
**Image image1 = new Image();
image1.Source = im.Source;
Canvas1.Children.Add(image1);**
}
}
Upvotes: 1
Views: 954
Reputation: 16662
There is the EditingMode property for doing all this.
On the following example you can drag an image to the window to add it to the canvas, then use the ComboBox to change the editing mode to something such as Select
to move any items in the canvas.
XAML:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350"
AllowDrop="True"
Drop="MainWindow_OnDrop">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Selection mode" />
<ComboBox Width="75"
Margin="2"
ItemsSource="{Binding}"
SelectionChanged="Selector_OnSelectionChanged" />
</StackPanel>
<InkCanvas x:Name="InkCanvas1"
Grid.Row="1"
MoveEnabled="True" />
</Grid>
</Window>
Code-behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WpfApplication8
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
IEnumerable<InkCanvasEditingMode> values =
Enum.GetValues(typeof (InkCanvasEditingMode)).Cast<InkCanvasEditingMode>();
DataContext = values;
}
private void MainWindow_OnDrop(object sender, DragEventArgs e)
{
object data = e.Data.GetData(DataFormats.FileDrop);
if (data != null)
{
var strings = data as string[];
if (strings != null)
{
string s = strings[0];
var bitmapImage = new BitmapImage(new Uri(s));
var image = new Image
{
Stretch = Stretch.None,
Source = bitmapImage
};
InkCanvas1.Children.Add(image);
}
}
}
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
object addedItem = e.AddedItems[0];
var inkCanvasEditingMode = (InkCanvasEditingMode) addedItem;
InkCanvas1.EditingMode = inkCanvasEditingMode;
}
}
}
Upvotes: 1