WPF Implementation of the drag-and-drop panel

Source: Internet
Author: User

The functions to be implemented here are similarBlendMiddle work panel-you can zoom in and drag the content.

There should be many ways to implement this function. In additionBlend 3AddedMousedragelementbehaviorThe implementation of this function seems simpler,MousedragelementbehaviorIs based onRendertransformTo achieve the drag effect. The result is that the scroll bar cannot be correctly displayed.

The following provides a relatively simple solution.CustomcontrolZooming and dragging are enabled, and the scroll bar can be displayed normally.

 

CodeAs follows:

Designviewer

Using System. windows;
Using System. Windows. controls;
Using System. Windows. Controls. primitives;
Using System. Windows. input;

Namespace Designview
{
///   <Summary>
///  
///   </Summary>
Public   Class Designviewer: contentcontrol
{
# Region Private Fields

PrivateThickness beginmargin;
PrivateViewbox;

# Endregion

# RegionConstructors

StaticDesignviewer ()
{
Defaultstylekeyproperty. overridemetadata (Typeof(Designviewer ),NewFrameworkpropertymetadata (Typeof(Designviewer )));
}

# Endregion

# RegionEvent Handlers

Public   Override   Void Onapplytemplate ()
{
Base . Onapplytemplate ();
Viewbox = Template. findname ( " Part_viewbox " , This ) As Viewbox;
Thumb = Template. findname ( " Part_thumb " , This ) As Thumb;

If (viewbox ! = null )
{< br> viewbox. previewmouseleftbuttondown += onthumbpreviewmouseleftbuttondown;
}

If (thumb ! = null )
{< br> thumb. dragdelta += ondragdelta;
thumb. dragstarted += ondragstarted;
}< BR >}

Private VoidOnthumbpreviewmouseleftbuttondown (ObjectSender, mousebuttoneventargs E)
{
E. Handled= True;
}

Private   Void Ondragdelta ( Object Sender, dragdeltaeventargs E)
{
Viewbox. Margin =   New Thickness ()
{
Top = Beginmargin. Top + E. verticalchange *   2 ,
Left = Beginmargin. Left + E. horizontalchange *   2
};
}

Private VoidOndragstarted (ObjectSender, dragstartedeventargs E)
{
Beginmargin=Viewbox. margin;
}

# Endregion
}
}

ItsStyleAs follows: 

 

Design viewer Style

< Resourcedictionary
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: Local = "CLR-namespace: designview" >
< Style X: Key = "Dragthumbstyle"
Targettype =" {X: Type thumb} " >
< Setter Property = "Stylus. ispressandholdenabled" Value = "False" />
< Setter Property = "Template" >
< Setter. Value >
< Controltemplate Targettype =" {X: Type thumb} " >
< Border Background = "#00000000" />
</ Controltemplate >
</ Setter. Value >
</ Setter >
</ Style >

< Controltemplate X: Key = "Designviewertemplate"
Targettype =" {X: type local: designviewer} " >
< Dockpanel >
< Slider X: Name = "Slider"
Value = "1" Maximum = "25"
Dockpanel. Dock = "TOP" />
< Scrollviewer Verticalscrollbarvisibility = "Auto"
Horizontalscrollbarvisibility = "Auto" >
< Grid >
< Thumb X: Name = "Part_thumb"
Style =" {Staticresource dragthumbstyle} " />
< Viewbox X: Name = "Part_viewbox" Stretch = "NONE"
Rendertransformorigin = "0.5, 0.5" >
< Viewbox. layouttransform >
< Scaletransform Scalex =" {Binding value, elementname = slider} "
Scaley =" {Binding value, elementname = slider} " />
</ Viewbox. layouttransform >
< Contentpresenter Content =" {Templatebinding content} " />
</ Viewbox >
</ Grid >
</ Scrollviewer >
</ Dockpanel >
</ Controltemplate >

< Style Targettype =" {X: type local: designviewer} " >
< Setter Property = "Template"
Value =" {Staticresource designviewertemplate} " />
</ Style >
</ Resourcedictionary >

 

 

Viewbox is used.AndSliderUsed to control scaling.ThumbControls control the drag and drop function, of course,ScrollviewerTo display the scroll bar.

 

CompleteProgramYou can download it here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.