Continue to study the WPF series.
First, let's take a look at the backgroundCodeAccess the front-end UI control.
This piece of knowledge is relatively old, no matter how we develop CS, BS has been used. while talking about the series of courses, I wrote all the code and content I want to talk about. posting a blog is just a change.
<Window X: class = "wpfuserpanel. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "obtain the XAML element The Day After Tomorrow" Height = "395" width = "395" borderthickness = "5" flowdirection = "lefttoright" grid. issharedsizmond = "false" showactivated = "false" background = "blanchedalmond">
<Window. Resources>
</Window. Resources>
<Grid>
<Grid. rowdefinitions>
<Rowdefinition/>
<Rowdefinition/>
</Grid. rowdefinitions>
<Grid. columndefinitions>
<Columndefinition width = "121 *" type = "codeph" text = "/codeph"/>
<Columndefinition width = "123 *" type = "codeph" text = "/codeph"/>
<Columndefinition width = "119 *" type = "codeph" text = "/codeph"/>
</Grid. columndefinitions>
<Textbox grid. columnspan = "3" margin = "27,47, 31,78" name = "textbox1" fontsize = "24"> Hello WPF is amazing </textbox>
<Button grid. row = "1" Height = "48" margin = "105,0, 108,0 "name =" button1 "verticalignment =" TOP "Click =" button?click "background =" sandybrown "borderthickness =" 1 "fontsize =" 18 "grid. columnspan = "3"> method 1 </button>
<Button margin = ", 54," name = "button2" grid. row = "1" Click = "button2_click" background = "sandybrown" borderthickness = "1" fontsize = "18" grid. columnspan = "3"> method 2 </button>
<Button margin = ", 0," name = "button3" grid. row = "1" Click = "button3_click" background = "sandybrown" borderthickness = "1" fontsize = "18" grid. columnspan = "3" Height = "42" verticalignment = "bottom"> method 3 </button>
</GRID>
</WINDOW>
There is nothing to say about the front-end XAML code.
Several access methods for background code.
Namespace wpfuserpanel {
Public partial class window1: window {
Public window1 (){
Initializecomponent ();
}
Void B _sizechanged (Object sender, sizechangedeventargs e ){
Throw new notimplementedexception ();
}
Private void button#click (Object sender, routedeventargs e ){
// The first access method. We can use the getvalue method to access it. Of course, textbox1.text is the simplest.
System. Windows. MessageBox. Show ("" + this. textbox1.getvalue (textbox. textproperty). tostring ());
}
Private void button2_click (Object sender, routedeventargs e ){
// This is similar to developing BS findcontrol to find the required control.
Textbox T = This. findname ("textbox1") as textbox;
System. Windows. MessageBox. Show ("Method 2:" + T. Text );
}
Private void button3_click (Object sender, routedeventargs e ){
// This is troublesome. Find the content first.
VaR v = This. content;
// Convert to the grid layout control and copy all the controls to the control array
Grid G = V as grid;
Uielementcollection UC = G. Children;
Control [] US = new control [uc. Count];
G. Children. copyto (US, 0 );
// Use LINQ to find the control. I believe everyone can understand the simple LINQ, and the level of my LINQ is also half-hanging .. I have been using linqtoobjects all the time. I haven't looked at anything else. I can simply use it in 20 minutes.
String STR = (US. First (A => A. Name = "textbox1") as textbox). text;
System. Windows. MessageBox. Show ("method 3:" + Str );
}
}
}
Let's start with a pure CS file of WPF.ProgramI believe that this is often the case when you are doing CS. I used to use only code to write CS programs without using forms.
Create a WPF Application and delete all the items. OK. Add the following code.
Namespace wpflayoutdemo
{
Class submain: system. Windows. Application
{
[Stathread] // specifies that the thread model of COM is a single thread base element.
Public static void main ()
{
System. Windows. Application APP = new application ();
Mywindow Mw = new mywindow ();
MW. width = 400;
MW. Height = 400;
MW. borderthickness = New thickness (50, 5, 50, 5 );
App. Run (MW); // start the application
}
}
Public partial class mywindow: Window
{
Canvas canv;
Ellipse E1;
Button B1;
Label lab1;
Rectangle R1;
Public mywindow ()
{
Canv = new canvas ();
Canv. Name = "C1 ";
This. content = canv; // make the current object equal to canvas
Canv. Margin = New thickness (0, 0, 0, 0 );
Canv. Background = new solidcolorbrush (colors. White );
E1 = new ellipse ();
E1.fill = new solidcolorbrush (colors. yellowgreen );
E1.stroke = new solidcolorbrush (colors. Azure );
E1.width = 200;
E1.height = 200;
E1.margin = New thickness (50,100, 0, 0 );
Canv. Children. Add (E1); // Let the canvas instance add an element
R1 = new rectangle ();
R1.fill = new solidcolorbrush (colors. Tomato );
R1.opacity = 0.5;
R1.stroke = new solidcolorbrush (colors. Red );
R1.width = 200;
R1.height = 200;
R1.setvalue (canvas. leftattribute, (double) 150 );
R1.setvalue (canvas. topproperty, (double) 100 );
Canv. Children. Add (R1 );
B1 = new button ();
B1.width = 100;
B1.height = 20;
B1.content = "modifying the circular position ";
B1.setvalue (canvas. leftproperty, (double) r1.getvalue (canvas. leftproperty) + 50 );
B1.setvalue (canvas. topproperty, (double) r1.getvalue (canvas. topproperty) + 50 );
B1.click + = new routedeventhandler (b1_click );
Canv. Children. Add (B1 );
Label lab0 = new label ();
Lab0.margin = New thickness (20, 20, 0, 0 );
Lab0.width = 400;
Lab0.height = 40;
Lab0.fontsize = 24;
Lab0.name = "lab0 ";
Lab0.content = "non-XAML Dynamic Programming demonstration by: changyong ";
Canv. Children. Add (lab0 );
Lab1 = new label ();
Lab1.margin = New thickness (20, 50, 0, 0 );
Lab1.width = 400;
Lab1.height = 40;
Lab1.fontsize = 24;
Lab1.name = "lab1 ";
Lab1.content = "Location :? ";
Canv. Children. Add (lab1 );
// This is the mousemove event of the ellipse instance object.
E1.mousemove + = new system. Windows. Input. mouseeventhandler (el_mousemove );
}
Void bid click (Object sender, routedeventargs E)
{
Point P = system. Windows. Input. Mouse. getposition (canv as system. Windows. iinputelement );
// Reset the ellipse location. The preceding two methods are described.
E1.margin = New thickness (P. X, p. Y, 0, 0 );
}
Void el_mousemove (Object sender, system. Windows. Input. mouseeventargs E)
{
Ellipse A = E. source as ellipse;
// Obtain the current mouse position. This is a class library reencapsulated by WPF. It uses system. Windows. Input. Mouse to obtain various mouse statuses.
Point P = system. Windows. Input. Mouse. getposition (canv
System. Windows. iinputelement );
Lab1.content = "Location:" + P. tostring ();//
}
}
}
In general, this small technology, widget search, and dynamic forms will be used in the future. It is also relatively simple and easy to understand.
Next we will learn styles. styles are similar to BS style sheets, but they are written differently.
Let's take a look at the XAML code.
<Window X: class = "WPF _ resource and style. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "window1" Height = "400" width = "400" loaded = "window_loaded">
<Window. Resources> // here I have defined static resources.
// Targettype = "{X: type button}" note that this is the style set for the button and cannot be used by other controls.
<Style X: Key = "greenbuttonstyle" targettype = "{X: type button}">
// Attribute styles start with setter
<Setter property = "control. width" value = "350"/>
<Setter property = "control. Height" value = "100" type = "codeph" text = "codeph"/>
<Setter property = "control. fontsize" value = "24"/>
// Event styles all start with eventsetter. The event handler is a method pointing to the background.
<Eventsetter handler = "button_click" event = "click"> </eventsetter>
</Style>
<Solidcolorbrush X: Key = "backgroundbrush"> yellow </solidcolorbrush>
<Solidcolorbrush X: Key = "borderbrush"> Red </solidcolorbrush>
</Window. Resources>
<Grid background = "{staticresource borderbrush}">
// We only need to use style to specify static resources.
<Button style = "{staticresource greenbuttonstyle}" background = "{staticresource backgroundbrush}"> Applications of WPF resources and styles </button>
<Label Height = "53" margin = "31,27, 41,0" name = "label1" verticalignment = "TOP" fontsize = "24"> Applications of Chinese source and style in WPF </label>
// This button uses dynamic resources, followed by the resource code
<Button content = "this is the style set through the dictionary item" margin = "17,0, 17, 73.163 "style =" {dynamicresource greybutton} "verticalalignment =" bottom "Height =" 35.837 "/>
</GRID>
</WINDOW>
// Resource dictionary code
<Resourcedictionary xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml">
<Style X: Key = "greybutton" targettype = "button">
<Setter property = "background" value = "#00 FFFF"/>
</Style>
</Resourcedictionary>
// Background code
Private void button_click (Object sender, routedeventargs E)
{
Global: system. Windows. MessageBox. Show ("WPF's XAML is really powerful! ");
}
We only have a button click event in the background. In general, styles are only written differently and there is no technical difficulty.
In this chapter, we will first understand some troublesome things.
After the dependency attributes, routing events, binding, and commands are explained, the basic part of WPF will come to an end.