Key Technical Analysis of WPF architecture (3)-making your own Interactive Actions (3)

Source: Internet
Author: User

1) test data preparation:

// This is used when I learned how to bind a Treeview. It also brings an example to netizens who are not very likely to bind a Treeview.
A) Hierarchical class, tree structure.

Public class folder
{
Public observablecollection <folder> children {Get; set ;}
Public String a {Get; set ;}
Public String B {Get; set ;}
}

B) A simple viewmodel

Public class mainviewmodel: viewmodelbase
{
Public String welcome
{
Get
{
Return "Welcome to mvvm light ";
}
}

/// <Summary>
/// Initializes a new instance of the mainviewmodel class.
/// </Summary>
Public mainviewmodel ()
{
If (isindesignmode)
{
// Code runs in Blend --> Create design time data.
}
Else
{
Loaddata ();
// Code runs "for real"
}
}
Public void loaddata ()
{
Observablecollection <folder> thefolders = new observablecollection <folder> ();
Folder thefolder = new folder ()
{
A = "",
B = "1001 ",
Children = new observablecollection <folder> (){
New Folder () {A = "B1", B = "101", children = new observablecollection <folder> (){
New Folder () {A = "C1", B = "10101 "},
New Folder () {A = "C2", B = "10102 "},
}},
New Folder () {A = "B2", B = "102", children = new observablecollection <folder> (){
New Folder () {A = "C3", B = "10201 "},
New Folder () {A = "C4", B = "10202 "},
}},
New Folder () {A = "B3", B = "103 "},
New Folder () {A = "B4", B = "104 "},
}

};
Thefolders. Add (thefolder );
Thefolder = new folder ()
{
A = "B ",
B = "20 ",
Children = new observablecollection <folder> (){
New Folder () {A = "B4", B = "201", children = new observablecollection <folder> (){
New Folder () {A = "C1", B = "20101 "},
New Folder () {A = "C2", B = "20102 "},
}},
New Folder () {A = "B5", B = "202", children = new observablecollection <folder> (){
New Folder () {A = "C3", B = "20201 "},
New Folder () {A = "C4", B = "20202 "},
}},
New Folder () {A = "B6", B = "203 "},
New Folder () {A = "B7", B = "204 "},
}

};
Thefolders. Add (thefolder );
This. Folders = thefolders;
}
Private observablecollection <folder> _ folders;
Public observablecollection <folder> folders
{
Get
{
Return _ folders;
}
Set
{
If (_ folders! = Value)
{
_ Folders = value;
Raisepropertychanged ("Folders ");
}
}
}
/// Public override void cleanup ()
////{
///// Clean up if needed

//// Base. Cleanup ();
////}
}

 

2) The following is the page:

<Usercontrol X: class = "mvvmlight1.mainpage"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: D = "http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
Xmlns: Local = "CLR-namespace: mvvmlight1" <! -- The namespace is defined here -->
MC: ignorable = "D"
Height = "300"
Width = "300"
Xmlns: I = "http://schemas.microsoft.com/expression/2010/interactivity"
Xmlns: Ei = "http://schemas.microsoft.com/expression/2010/interactions"
Datacontext = "{binding main, source = {staticresource locator }}"
Xmlns: SDK = "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

<Usercontrol. Resources>

<! -- Treeview template -->
<SDK: hierarchicaldatatemplate X: Key = "foldertemplate" itemssource = "{binding children}">
<Grid>
<Grid. columndefinitions>
<Columndefinition width = "0.3 *"> </columndefinition>
<Columndefinition width = "0.7 *"> </columndefinition>
</Grid. columndefinitions>
<Textblock grid. Column = "0" text = "{binding Path = A}"> </textblock>
<Textbox grid. Column = "1" text = "{binding Path = B}"> </textbox>
</GRID>

</SDK: hierarchicaldatatemplate>
</Usercontrol. Resources>

<Grid X: Name = "layoutroot">

<Textblock fontsize = "36"
Fontweight = "bold"
Foreground = "Purple"
TEXT = "{binding welcome }"
Verticalalignment = "center"
Horizontalalignment = "center"
Textwrapping = "Wrap"/>
<SDK: Treeview itemssource = "{binding Path = folders}" itemtemplate = "{staticresource foldertemplate}" horizontalalignment = "stretch" margin = "0, 0, 0, 0 "name =" treeview1 "verticalignment =" stretch "selecteditemchanged =" treeview1_selecteditemchanged ">
<! -- <I: interaction. behaviors> Microsoft behavior is used here.
<Local: mybehavior Params = "Hello, Tian" targetname = "treeview1" methodname = "treeviewselected"/>
</I: interaction. behaviors> -->

<! -- The Interaction treeviewselected selection method is defined on this page using the action framework we just defined. see the following code in CS. Pay attention to the myactiontest parameters. These parameters will eventually become the value of the corresponding attribute during parsing .. -->
<Local: wpftestdettach. myobjects>
<Local: myactiontest eventname = "selecteditemchanged" Params = "Hello, 1" targetname = "" methodname = "treeviewselected" myname = "XXX1"> </local: myactiontest>
<Local: myactiontest eventname = "selecteditemchanged" Params = "Hello, 2" targetname = "" methodname = "treeviewselected" myname = "xxx2"> </local: myactiontest>
<Local: myactiontest eventname = "selecteditemchanged" Params = "Hello, 3" targetname = "" methodname = "treeviewselected" myname = "xxx3"> </local: myactiontest>
<Local: myactiontest eventname = "selecteditemchanged" Params = "Hello, 4" targetname = "" methodname = "treeviewselected" myname = "xxx4"> </local: myactiontest>
</Local: wpftestdettach. myobjects>
</SDK: Treeview>
</GRID>
</Usercontrol>

3) XAML. CS

Public partial class mainpage: usercontrol
{
Public mainpage ()
{
Initializecomponent ();

} Public void treeviewselected (string Params)
{
MessageBox. Show (Params );
}
}

 

4) I implemented it using Microsoft's behaviors and mybehavior inherited from behavior <>. You can compare it with many similar codes.

Public class mybehavior: behavior <Treeview>
{
Protected override void onattached ()
{
Base. onattached ();
This. associatedobject. selecteditemchanged + = new routedpropertychangedeventhandler <Object> (associatedobject_selecteditemchanged );
}

Void associatedobject_selecteditemchanged (Object sender, routedpropertychangedeventargs <Object> E)
{
Dosomething (Params );
}
Protected override void ondetaching ()
{
Base. ondetaching ();
This. associatedobject. selecteditemchanged-= new routedpropertychangedeventhandler <Object> (associatedobject_selecteditemchanged );

}
Public String Params {Get; set ;}
Public String targetname {Get; set ;}
Public String methodname {Get; set ;}
Public void dosomething (string PMS)
{

Object therootobj = getrootobject (this. associatedobject );
Object thetargetobj = NULL;
If (targetname = "")
{
Thetargetobj = therootobj;
}
Else
{
Thetargetobj = (frameworkelement) therootobj). findname (targetname );
}
Myactioncollection thevalue = (myactioncollection) (dependencyobject) thetargetobj). getvalue (wpftestdettach. myobjectsproperty );
Foreach (VAR item in thevalue)
{
String A = item. myname;
}
Methodinfo [] Themis = thetargetobj. GetType (). getmethods ();
Methodinfo themI = thetargetobj. GetType (). getmethod (methodname, bindingflags. nonpublic );
If (themI! = NULL)
{
ThemI. Invoke (thetargetobj, null );
}

}
Private frameworkelement getrootobject (frameworkelement OBJ)
{
If (obj. parent! = NULL & obj. parent is frameworkelement)
{
Return getrootobject (frameworkelement) (obj. Parent ));
}
Return OBJ;
}
}

 

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.