I'm trying to create Diagram Designer with WPF and using the MVVM pattern, I take information and some tips from this guide: https://www.codeproject.com/Articles/22952/WPF-Diagram-Designer-Part-1
And in one moment, my project looks like:
WPF: Hydrate Canvas with Draggable Controls at Runtime
And of course I had the similar problem as the author mentioned above: when I draw my ContentControl
, it's drawing correctly with random coordinates, but when I try to move it, it won't move! And when I debug the class MoveThumb
, I see that my ContentControl
has not got its Parent
. But in my opinion, it should have Canvas
as a Parent
. I understand that I should override some system/basic method, but I can't understand what and how should I override it. Maybe someone has an idea?
Now I try to describe my implementation, first i create BaseShapeViewModel
abstract public class BaseShapeViewModel : BaseViewModel
{
public BaseShapeViewModel()
{
}
private double left;
public double Left
{
get => left;
set => SetField(ref left, value, nameof(Left));
}
private double top;
public double Top
{
get => top;
set => SetField(ref top, value, nameof(Top));
}
private int width;
public int Width
{
get => width;
set => SetField(ref width, value, nameof(Width));
}
private int height;
public int Height
{
get => height;
set => SetField(ref height, value, nameof(Height));
}
private string fill;
public string Fill
{
get => fill;
set => SetField(ref fill, value, nameof(Fill));
}
private string text;
public string Text
{
get => text;
set => SetField(ref text, value, nameof(Text));
}
}
other ViewModels EllipseViewModel, RectangleViewModel inherit from BaseShapeViewModel. My MainViewModel looks like
class MainViewModel : BaseViewModel
{
public MainViewModel()
{
BaseShapeViewModels = new ObservableCollection<BaseShapeViewModel>();
}
public ObservableCollection<BaseShapeViewModel> BaseShapeViewModels { get; set; }
//public Canvas DesignerCanvas;
private RelayCommand createUseCase;
private RelayCommand createRectangle;
public ICommand CreateUseCase
{
get
{
return createUseCase ??
(
createUseCase = new RelayCommand(() => { AddUseCase(); })
);
}
}
public ICommand CreateRectangle
{
get
{
return createRectangle ??
(
createRectangle = new RelayCommand(() => { AddRectangle(); })
);
}
}
private void AddUseCase()
{
Random rnd = new Random();
int valueLeft = rnd.Next(0, 200);
int valueTop = rnd.Next(0, 200);
EllipseViewModel useCaseViewModel = new EllipseViewModel {Left=valueLeft,Top=valueTop, Height = 100, Width = 200, Fill="Blue"};
BaseShapeViewModels.Add(useCaseViewModel);
}
private void AddRectangle()
{
Random rnd = new Random();
int valueLeft = rnd.Next(0, 200);
int valueTop = rnd.Next(0, 200);
RectangleViewModel rectangleViewModel = new RectangleViewModel { Left = valueLeft, Top = valueTop, Height = 100, Width = 200, Fill = "Blue" };
BaseShapeViewModels.Add(rectangleViewModel);
}
}
My MoveThumb.cs looks like
public class MoveThumb : Thumb
{
public MoveThumb()
{
DragDelta += new DragDeltaEventHandler(this.MoveThumb_DragDelta);
}
private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
ContentControl designerItem = DataContext as ContentControl;
if (designerItem != null)
{
Point dragDelta = new Point(e.HorizontalChange, e.VerticalChange);
RotateTransform rotateTransform = designerItem.RenderTransform as RotateTransform;
if (rotateTransform != null)
{
dragDelta = rotateTransform.Transform(dragDelta);
}
double left = Canvas.GetLeft(designerItem);
double top = Canvas.GetTop(designerItem);
Canvas.SetLeft(designerItem, left + dragDelta.X);
Canvas.SetTop(designerItem, top + dragDelta.Y);
}
}
}
And know i want to say that im not profi at Xaml, but i check material from the first link and create MoveThumb.xaml like this
<ResourceDictionary
<ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type s:MoveThumb}">
<Rectangle Fill="Transparent"/>
</ControlTemplate>
after i create ResizeDecorator and RotateDecorator, but right now it doesnt matter, and create DesignerItem.xaml
ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MoveThumb.xaml"/>
<ResourceDictionary Source="ResizeDecorator.xaml"/>
<ResourceDictionary Source="RotateDecorator.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- ContentControl style to move, resize and rotate items -->
<Style x:Key="DesignerItemStyle" TargetType="ContentControl">
<Setter Property="MinHeight" Value="50"/>
<Setter Property="MinWidth" Value="50"/>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<Control Name="RotateDecorator"
Template="{StaticResource RotateDecoratorTemplate}"
Visibility="Collapsed"/>
<s:MoveThumb Template="{StaticResource MoveThumbTemplate}"
Cursor="SizeAll"/>
<Control x:Name="ResizeDecorator"
Template="{StaticResource ResizeDecoratorTemplate}"
Visibility="Collapsed"/>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter TargetName="ResizeDecorator" Property="Visibility" Value="Visible"/>
<Setter TargetName="RotateDecorator" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And i try to Bind this Style in my MainWindow.xaml for my ContentControls
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/DesignerItem.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Background="Gray" Grid.RowSpan="2">
<TextBlock Text="Shapes"
FontSize="18"
TextAlignment="Center" />
<Button Content="Initial Node" />
<Button Content="Final Node" />
<Button Content="Line" />
<Button Content="Action" />
<Button Content="Decision Node" />
<Button Content="Actor" />
<Button Content="Class" Command="{Binding Path=CreateRectangle}"/>
<Button Content="Use Case" Command="{Binding Path = CreateUseCase}"/>
</StackPanel>
<Grid Grid.Column="2">
<ItemsControl ItemsSource="{Binding Path= BaseShapeViewModels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type viewModel:EllipseViewModel}">
<ContentControl
Selector.IsSelected="True"
Style="{StaticResource DesignerItemStyle}">
<Ellipse
Fill="{Binding Fill}"
IsHitTestVisible="False"/>
</ContentControl>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:RectangleViewModel}">
<ContentControl
Selector.IsSelected="True"
Style="{StaticResource DesignerItemStyle}">
<Rectangle
Fill="{Binding Fill}"
IsHitTestVisible="False"/>
</ContentControl>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Left, Mode=TwoWay}"/>
<Setter Property="Canvas.Top" Value="{Binding Top, Mode=TwoWay}"/>
<Setter Property="Width" Value="{Binding Width, Mode=TwoWay}"/>
<Setter Property="Height" Value="{Binding Height,Mode=TwoWay}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Grid>