abbas akbarzade
abbas akbarzade

Reputation: 33

Add Rectangle to ContentControl C# wpf

I have a Xaml Code in My Project Like This:

    <Window.Resources>

        <!-- MoveThumb Template -->
        <ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type local:MoveThumb}">
            <Rectangle Fill="Transparent"/>
        </ControlTemplate>

        
        <!-- ResizeDecorator Template -->
        <ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
            <Grid>
                <local:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0"
                       VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
                <local:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0"
                       VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
                <local:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0"
                       VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
                <local:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4"
                       VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
                <local:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0"
                       VerticalAlignment="Top" HorizontalAlignment="Left"/>
                <local:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0"
                       VerticalAlignment="Top" HorizontalAlignment="Right"/>
                <local:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6"
                       VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
                <local:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6"
                       VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
            </Grid>
        </ControlTemplate>


        <!-- Designer Item Template-->
        <ControlTemplate x:Key="DesignerItemTemplate" TargetType="ContentControl">
            <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                <local:MoveThumb Template="{StaticResource MoveThumbTemplate}" Cursor="SizeAll"/>
                <Control Template="{StaticResource ResizeDecoratorTemplate}"/>
                <ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
            </Grid>
        </ControlTemplate>
    </Window.Resources>

....
<Canvas x:Name="Container" Background="Transparent">**strong text**
    <ContentControl
         Width="130" MinWidth="50" Height="130"
         MinHeight="50" Canvas.Top="150" Canvas.Left="150"
         Template="{StaticResource DesignerItemTemplate}">
              <Rectangle Fill="AliceBlue" Opacity="0.5" Margin="10"  Stroke="Blue" StrokeThickness="1" RadiusX="0" />
    </ContentControl>
</Canvas

MoveThumb.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace Test_Canvas_Rectangle
{
    class MoveThumb: Thumb
    {
        public MoveThumb()
        {
            DragDelta += new DragDeltaEventHandler(this.MoveThumb_DragDelta);
        }

        private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Control designerItem = this.DataContext as Control;

            if (designerItem != null)
            {
                double left = Canvas.GetLeft(designerItem);
                double top = Canvas.GetTop(designerItem);

                Canvas.SetLeft(designerItem, left + e.HorizontalChange);
                Canvas.SetTop(designerItem, top + e.VerticalChange);
            }
        }
    }
}

ResizeThumb.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace Test_Canvas_Rectangle
{
    class ResizeThumb : Thumb
    { 
        public ResizeThumb()
    {
        DragDelta += new DragDeltaEventHandler(this.ResizeThumb_DragDelta);
    }

    private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
    {
        Control designerItem = this.DataContext as Control;

        if (designerItem != null)
        {
            double deltaVertical, deltaHorizontal;

            switch (VerticalAlignment)
            {
                case VerticalAlignment.Bottom:
                    deltaVertical = Math.Min(-e.VerticalChange, designerItem.ActualHeight - designerItem.MinHeight);
                    designerItem.Height -= deltaVertical;
                    break;
                case VerticalAlignment.Top:
                    deltaVertical = Math.Min(e.VerticalChange, designerItem.ActualHeight - designerItem.MinHeight);
                    Canvas.SetTop(designerItem, Canvas.GetTop(designerItem) + deltaVertical);
                    designerItem.Height -= deltaVertical;
                    break;
                default:
                    break;
            }

            switch (HorizontalAlignment)
            {
                case HorizontalAlignment.Left:
                    deltaHorizontal = Math.Min(e.HorizontalChange, designerItem.ActualWidth - designerItem.MinWidth);
                    Canvas.SetLeft(designerItem, Canvas.GetLeft(designerItem) + deltaHorizontal);
                    designerItem.Width -= deltaHorizontal;
                    break;
                case HorizontalAlignment.Right:
                    deltaHorizontal = Math.Min(-e.HorizontalChange, designerItem.ActualWidth - designerItem.MinWidth);
                    designerItem.Width -= deltaHorizontal;
                    break;
                default:
                    break;
            }
        }

        e.Handled = true;
    }

    }

}

actually I want to add Rectangle using C# Side using mouse move Like This:

private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var Limit = (FrameworkElement)sender;

            // Set left mouse button state to released
            _LeftMouseHeld = false;


            // Hide all the listbox (if you forget to specify width and height you will have remanent coordinates
            SelectBox.Visibility = Visibility.Collapsed;
            SelectBox.Width = 0;
            SelectBox.Height = 0;

            Random rnd = new Random(Guid.NewGuid().ToString().GetHashCode());
            CC = new ContentControl()
            {
                Width = width,
                Height = height,
                Template = DesignerItemTemplate,
            };

            rec = new Rectangle()
            {
                Fill = PickRandomBrush(rnd),
                Opacity = 0.5,
            };

            //CC.Content = rec;
            //CC.Content = rec;
            
            Container.Children.Add(CC);

            if (currentPos.X > _InitPos.X)
            {
                Canvas.SetLeft(rec, _InitPos.X);
                rec.Width = currentPos.X - _InitPos.X;
            }
            else
            {
                Canvas.SetLeft(rec, currentPos.X);
                rec.Width = _InitPos.X - currentPos.X;
            }

            // Y coordinates
            if (currentPos.Y > _InitPos.Y)
            {
                Canvas.SetTop(rec, _InitPos.Y);
                rec.Height = currentPos.Y - _InitPos.Y;
            }
            else
            {
                Canvas.SetTop(rec, currentPos.Y);
                rec.Height = _InitPos.Y - currentPos.Y;
            }

            Limit.ReleaseMouseCapture();

        }

The problem is I can not display The rectangle and conrolTemplate in My app. I tried to show Rectangles using Container.Children.Add(rec); and it shows the result but I can not add rectangles to my control Item. Thanks for Helping

Upvotes: 0

Views: 66

Answers (1)

abbas akbarzade
abbas akbarzade

Reputation: 33

I found a very useful code sample and customized it in my own style. This sample can add different shapes to a canvas and gives the user the ability to change the size and select and move during runtime.

By removing some Feachers and changing colors, as well as removing some additional features, we get the correct result of the need

Upvotes: 0

Related Questions