[Original] algorithm recursion (4)-Application

Source: Internet
Author: User

Recently with a few in the making of a project, the UI layer is using WPF. There was little in-depth contact with WPF before, but when contacted, WPF was found to be powerful.

At least the user experience on the interface design has improved significantly compared to the WinForm.

You need to generalize several styles in your project and assign the styles to the appropriate controls. The control is dynamically generated from the configuration file, which is a more hierarchical structure in XML format. Therefore, in the dynamic generation process, the recursive approach is implemented.

Here is a mock example.

Goal:

Add all the types of the assembly "PresentationFramework" to the TreeView, and if a type has a base class row, first join the base type, and so on.

Practice:

1. Create a WPF application, add an XML file, and place it in the root of the project. (Don't put it under the OutputDir).

2. In this XML file, add the following code and save it.

<Stylexmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:l= "Clr-namespace:system.windows.controls;assembly:presentationframework"TargetType="{x:type Typename=treeviewitem}">  <Setter Property= "Foreground"Value= "Yellow" />  <Setter Property= "FontFamily"Value= "Comic Sans MS" />  <Setter Property= "FontSize"Value= "+" />  <style.triggers>    <Trigger Property= "IsMouseOver"Value= "True">      <Setter Property= "Background"Value= "Darkblue" />    </Trigger>  </style.triggers></Style>

3. Create a style parser in the CS file of the MainWindow. The code is as follows.

        Private void Styleparser ()        {            usingnew FileStream (this. Stylepath, FileMode.Open, FileAccess.Read))            {                this. MyStyle = (Style) xamlreader.load (FileStream);            }        } 

4. Add all types of "PresentationFramework" to the TreeView in a recursive form.

(1) define a recursive algorithm.

        Private void Appenditem (ItemsControl Ctrl, type type)        {            ifnull)                 return ;             New  This . MyStyle};            Ctrl. Items.Add (Mytreeviewitem);            Appenditem (Mytreeviewitem, type. BaseType);        }

(2) Initialize the TreeView and call the Appenditem method.

        Private voidWindow_loaded_1 (Objectsender, RoutedEventArgs e)            {relayout (); Mytreeview.background=Brushes.seagreen; Mytreeview.horizontalalignment=System.Windows.HorizontalAlignment.Left; Mytreeview.verticalalignment=System.Windows.VerticalAlignment.Top; Mytreeview.margin=NewThickness (5,5,0,0);  This. MAINGRID.CHILDREN.ADD (MyTreeView); varASM = assembly.getassembly (typeof(TreeViewItem)); foreach(varTypeinchASM. Exportedtypes) {varnode =NewTreeViewItem () {Header = type. FullName, Style = This. MyStyle};                MYTREEVIEW.ITEMS.ADD (node);            Appenditem (node, type); }        }

5. Define a Relayout method to adjust the layout of the TreeView control in the main form. This method is called in the "SizeChanged", "statechanged" practice.

        Private voidrelayout () {if( This. ActualHeight < -)                return; if( This. ActualWidth < -)                return; Mytreeview.height= This. ActualHeight- -; Mytreeview.width= This. ActualWidth/2; }

6. Operation, the effect is as follows.

The complete code is as follows.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;usingSystem.Windows.Markup;usingSystem.IO;usingSystem.Reflection;namespacewpfapplication8{/// <summary>    ///Interaction logic for MainWindow.xaml/// </summary>     Public Partial classMainwindow:window {PrivateStyle MyStyle; Private stringStylepath =".. \\.. \\CustomizedStyle.xml"; PrivateTreeView MyTreeView =NewTreeView ();  PublicMainWindow () {InitializeComponent ();  This.        Styleparser (); }        Private voidStyleparser () {using(FileStream FileStream =NewFileStream ( This. Stylepath, FileMode.Open, FileAccess.Read)) {                 This. MyStyle =(Style) xamlreader.load (FileStream); }        }        Private voidAppenditem (ItemsControl Ctrl, type type) {if(Type. BaseType = =NULL)                return; TreeViewItem Mytreeviewitem=NewTreeViewItem () {Header = type. FullName, Style = This. MyStyle}; Ctrl.            Items.Add (Mytreeviewitem); Appenditem (Mytreeviewitem, type.        BaseType); }        Private voidWindow_loaded_1 (Objectsender, RoutedEventArgs e)            {relayout (); Mytreeview.background=Brushes.seagreen; Mytreeview.horizontalalignment=System.Windows.HorizontalAlignment.Left; Mytreeview.verticalalignment=System.Windows.VerticalAlignment.Top; Mytreeview.margin=NewThickness (5,5,0,0);  This. MAINGRID.CHILDREN.ADD (MyTreeView); varASM = assembly.getassembly (typeof(TreeViewItem)); foreach(varTypeinchASM. Exportedtypes) {varnode =NewTreeViewItem () {Header = type. FullName, Style = This. MyStyle};                MYTREEVIEW.ITEMS.ADD (node);            Appenditem (node, type); }        }        Private voidrelayout () {if( This. ActualHeight < -)                return; if( This. ActualWidth < -)                return; Mytreeview.height= This. ActualHeight- -; Mytreeview.width= This. ActualWidth/2; }        Private voidWindow_statechanged_1 (Objectsender, EventArgs e)        {relayout (); }        Private voidWindow_sizechanged_1 (Objectsender, Sizechangedeventargs e)        {relayout (); }    }}

[Original] algorithm recursion (4)-Application

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.