To create a WPF user control

Source: Internet
Author: User

WPF user-defined controls and WinForm creation methods are similar, here first to correct a misunderstanding, that is, a lot of people are added, and then new, and then the new user Control Library, but why compiled after the debug directory or only EXE file and no DLL file? Here you have to note that the new to right-click in the solution New-new project, not under the project new, because a project only produces an EXE program file, to generate a DLL file must create a new project.

Here I use a ToolTip to create a new user control:

Create a new WPF program, which I'll name here: "WPF Custom Control." Then create a new project, the WPF user Control Library, which I name here: "WpfControlLibrary1".

After that, add the following code to the XAML file for the new project "WpfControlLibrary1":

<usercontrol x:class= "Wpfcontrollibrary1.usercontrol1" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/ Presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:mc=" Http://schemas.open              xmlformats.org/markup-compatibility/2006 "xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "              mc:ignorable= "D" rendertransformorigin= "0,0" horizontalalignment= "left" verticalalignment= "Top"        d:designheight= "d:designwidth=" name= "User custom Controls" > <UserControl.RenderTransform> <TransformGroup> <scaletransform scalex= "1" scaley= "1"/> <skewtransform anglex= " 0 "angley=" 0 "/> <rotatetransform angle=" 0 "/> <translatetransform x:name=" Usercontroltool Tipxy "x=" 0 "y=" 0 "/> </TransformGroup> </UserControl.RenderTransform> <grid Horizontalalig Nment= "Center" VerticalaLignment= "Center" minwidth= "minheight=" > <Grid.RowDefinitions> <rowdefinition H eight= "0.333*"/> <rowdefinition height= "0.667*"/> </Grid.RowDefinitions> <recta Ngle fill= "#FFFBFBFB" stroke= "#FF000000" radiusx= "" radiusy= "rendertransformorigin= hickness= "1" grid.rowspan= "2" > <Rectangle.BitmapEffect> <dropshadowbitmapeffect Opac ity= "0.8"/> </Rectangle.BitmapEffect> </Rectangle> <rectangle radiusx= "Ten" Radi Usy= "rendertransformorigin=" 0.139,0.012 "strokethickness=" stroke= "{x:null}" margin= "1,1,1,1" Grid. row= "0" grid.rowspan= "2" > <Rectangle.Fill> <lineargradientbrush endpoint= "0.5,1" Sta Rtpoint= "0.5,0.725" > <gradientstop color= "#00E6D9AA" offset= "0.487"/> <g Radientstop color= "#FF001EFF "offset=" 0.996 "/> </LinearGradientBrush> </Rectangle.Fill> </recta Ngle> <rectangle radiusx= "Ten" radiusy= "rendertransformorigin=" 0.493,0.485 "strokethickness=" 1 0 "stroke=" {x:null} "grid.rowspan=" 2 "margin=" 1,1,1,1 "> <Rectangle.Fill> <lineargrad Ientbrush endpoint= "0.014,0.5" startpoint= "0.211,0.5" > <gradientstop color= "#00E6D9AA" offset= "0. 513 "/> <gradientstop color=" #FF001EFF "offset=" 0.996 "/> </lineargradientbrus h> </Rectangle.Fill> </Rectangle> <rectangle radiusx= "Ten" radiusy= "RENDERTR"            Ansformorigin= "0.493,0.485" strokethickness= "ten" stroke= "{x:null}" grid.rowspan= "2" margin= "1,1,1,1" >                    <Rectangle.Fill> <lineargradientbrush endpoint= "0.493,0.002" startpoint= "0.493,0.33" > <gradIentstop color= "#00E6D9AA" offset= "0.513"/> <gradientstop color= "#FF001EFF" offset= "0.996"/> </LinearGradientBrush> </Rectangle.Fill> </Rectangle> <rectang Le radiusx= "ten" radiusy= "rendertransformorigin=" 0.493,0.485 "strokethickness=" "stroke=" {x:null} "Grid.R Owspan= "2" margin= "1,1,1,1" > <Rectangle.Fill> <lineargradientbrush endpoint= "0.99,0.                    441 "startpoint=" 0.794,0.441 "> <gradientstop color=" #00E6D9AA "offset=" 0.513 "/> <gradientstop color= "#FF001EFF" offset= "0.996"/> </LinearGradientBrush> </re Ctangle. fill> </Rectangle> <textblock text= "hello! "Textwrapping=" Wrap "x:name=" Textblocktooltip "rendertransformorigin=" 0.5,0.5 "grid.row=" 1 "horizontalalignme Nt= "left" verticalalignment= "Center" margin= "20,0,0,20"/> <textblock name=" Tooltiptitle "horizontalalignment=" Stretch "margin=" 15,16,15,6.1 "fontsize= text= "user-defined control" d:layoutoverrides= "Height"/> </Grid></UserControl>

  

Effect

Below we add the following code in the CS file to control:

Public double usercontroltooltipx        {            get {return this. usercontroltooltipxy.x; }            set {this. usercontroltooltipxy.x = value; }        } public         double usercontroltooltipy        {            get {return this. USERCONTROLTOOLTIPXY.Y; }            set {this. USERCONTROLTOOLTIPXY.Y = value; }        } public         string Usercontroltextblocktooltip        {            get {return textblocktooltip.text;}            set {Textblocktooltip.text = value;}        }         public string Usercontroltooltiptitle        {            get {return tooltiptitle.text;}            set {Tooltiptitle.text = value;}        }

When we're done we can see that the toolbox on the left has the custom controls we just added,

Then we click on the Generate user control in the build, or the shortcut key Shift+f6,

Finally, in our debug directory, we have just created the DLL file.

To create a WPF user control

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.