Hello,
I'm using a RadDiagram where the users can add images and draw arrows.
By default, when the user draws an arrow over a shape, the arrow connects to the closest connection point. I need to modify this behavior. When the ActiveTool is ConnectorTool and the user clicks on the image, the arrow must start from the point where the user clicked, and not from the closest connection point.
In order to achieve this, I removed all connection points from the image when I add it. This didn't work as expected.
The following code sample allows me te reproduce the error. When I click outside the image, the behavior is nominal.
When I click on the image, an exception is thrown. Instead, I want the application to behave as if the image wasn't there.
In my sample, the ActiveTool is always ConnectorTool, but in my real application, the ActiveTool can have other values. This behavior must only apply to the ConnectorTool. For example, when the PointerTool is active, the user must be able to select and move the image.
class
Program
{
[STAThread]
static
void
Main(
string
[] args)
{
TestWindow win =
new
TestWindow();
win.ShowDialog();
}
}
class
TestWindow : Window
{
public
TestWindow()
{
Grid main_grid =
new
Grid();
main_grid.ColumnDefinitions.Add(
new
ColumnDefinition() { Width = GridLength.Auto });
main_grid.ColumnDefinitions.Add(
new
ColumnDefinition() { Width =
new
GridLength(1, GridUnitType.Star) });
RadDiagram my_diagram =
new
RadDiagram();
my_diagram.ActiveTool = Telerik.Windows.Diagrams.Core.MouseTool.ConnectorTool;
Grid.SetColumn(my_diagram, 1);
ViewModel vm =
new
ViewModel(my_diagram);
Button button_add_image =
new
Button();
button_add_image.Content =
"Add image"
;
button_add_image.Command = vm.CommandAddImage;
AddChild(main_grid);
main_grid.Children.Add(my_diagram);
main_grid.Children.Add(button_add_image);
DataContext = vm;
}
}
class
ViewModel : ViewModelBase
{
public
ViewModel(RadDiagram diagram)
{
MyDiagram = diagram;
}
public
RadDiagram MyDiagram {
get
;
set
; }
public
ICommand CommandAddImage {
get
{
return
new
ICommandImpl(AddImage); } }
public
void
AddImage()
{
Microsoft.Win32.OpenFileDialog open_file_dialog =
new
Microsoft.Win32.OpenFileDialog();
open_file_dialog.Filter =
"Image Files (*.png, *.jpg, *.bmp)|*.png;*.jpg;*.bmp"
;
open_file_dialog.CheckFileExists =
true
;
if
(!open_file_dialog.ShowDialog().GetValueOrDefault() || !System.IO.File.Exists(open_file_dialog.FileName))
{
return
; }
System.Windows.Media.Imaging.BitmapImage image_source =
new
System.Windows.Media.Imaging.BitmapImage(
new
Uri(open_file_dialog.FileName));
System.Windows.Controls.Image image_displayed =
new
System.Windows.Controls.Image();
image_displayed.Source = image_source;
Viewbox viewbox_added_to_diagram =
new
Viewbox();
viewbox_added_to_diagram.Stretch = Stretch.Fill;
viewbox_added_to_diagram.Margin =
new
System.Windows.Thickness(-4);
viewbox_added_to_diagram.Child = image_displayed;
RadDiagramShape shape_added_to_diagram =
new
RadDiagramShape();
shape_added_to_diagram.Content = viewbox_added_to_diagram;
shape_added_to_diagram.Background = Brushes.Transparent;
shape_added_to_diagram.BorderBrush = Brushes.Green;
shape_added_to_diagram.Connectors.Clear();
MyDiagram.AddShape(shape_added_to_diagram,
new
Point(20, 20));
}
private
class
ICommandImpl : ICommand
{
private
Action Command;
public
ICommandImpl(Action p_action) { Command = p_action; }
public
void
Execute(
object
parameter) { Command(); }
public
bool
CanExecute(
object
parameter) {
return
true
; }
public
event
EventHandler CanExecuteChanged;
}
}
I followed the instructions in another post, but I didn't manage to make it work.
I used added the following tool, but the behavior is not what I need. Now, when I click on a shape, nothing happens. I want a new connection to start when I click on a shape.
class CustomConnectionTool : ConnectionTool
{
public override bool MouseDown(PointerArgs e)
{
if (!this.IsActive) return false;
var hitItem = this.HitTestService.ItemUnderMouse as IShape;
if (hitItem == null || hitItem.Connectors.Count > 0)
return base.MouseDown(e);
else
return false;
}
}
The post suggested creating a new service or a custom tool, but I couldn't figure it out. In addition, my real application has more styles and custom behavior associated with Connections, so I'd rather not use a different class.
How else can I achieve what I need ? When the user clicks a shape, it must not start from the closest connection, but must instead start from the point where the user clicked.