📜 ⬆️ ⬇️

Displaying a hierarchical data structure in WPF using anchors and templates

Introduction

Representing a dataset as a hierarchical structure (of any nesting level) in WPF is very simple. As a rule, the System.Windows.Controls.TreeView class is used for this and the result looks like this:


I will demonstrate two cases of building such a tree, differing from each other by a data source :


The application will implement the ability to dynamically replace one data source with another.
First of all, it is necessary to create data sources from which information should be obtained. Such sources will be:
  1. MyTestDb.mdf database
  2. XMLFile1.xml XML file

This will create a single solution (Solution) consisting of two projects (Projects):
  1. Linq2SqlProject is a library containing a set of classes that are the object projection of the relational database structure.
  2. WpfGuiProject - a graphical part of the application (GUI), made using WPF technology. In the same project will be posted code relating to working with XML-data.

Interaction with all data sources considered in the topic will be implemented using LINQ technology.

1. Creating a database


In our example, the database will consist of two tables - this is enough to demonstrate the solution of the voiced task:
')


I added the following entries to the MySchema.Categories table:



The following data has been entered into the MySchema.Books table:



In order not to complicate the example, I will not create a set of stored procedures in the database, with the help of which the client application should work with the database (although in real work, of course, you should conduct a dialogue with the database only through stored procedures to avoid the possibility of sql injection).
Download the above database from here and connect it to our database.

2. Create an object-oriented projection database



1. Create a new empty solution (Solution), giving it the name "TreeStructureBrowse":



2. From the context menu, obtained by clicking on the solution name in Solution Explorer, select Add => New Project ...
3. Select the project type “Class Library” and assign the name “Linq2Sql” to our project, then press the OK key.
4. From the context menu, obtained by clicking the mouse on the name of the project that we added in step 3, select the Add => New Item item ...
5. Select the LINQ to SQL Classes item and assign the file name “MyTestDb” to the file:



6. From the menu "View", select the item "Server Explorer". In the window that appears, click "Connect to Database":



7. Specify all the necessary parameters of the connection string (in my case Windows authentication is used):



8. Click the “Test Connection” button to make sure that we can connect, then click the OK button to close the “Add Connection” window.
9. Now in the Server Explorer window we will see the following picture:



ps powercomp is the name of my computer

10. Hold down the Shift key, select both our tables (Books and Categories) and drag them to the MyTestDb.dbml * tab, with the result that we get the following picture:



11. Save our project and execute the command Build => Rebuild Solution.

So we have just created an object-oriented model of our relational database , which is the first key point (of the three) in solving our problem.

3. Creating a graphic part (GUI), writing converters and generating templates


1. In the solution we created in the previous section, we will add a new project, giving it the name WpfGuiProject:



2. From the context menu, caused by right-clicking on the name of the project that was just added to our solution, select the “Set as StartUp Project” item, thus making the GUI project run by default.
3. Add a link to the Linq2Sql project to the WpfGuiProject project:



4. Add a resource dictionary to our project: from the context menu, called by clicking the right mouse button on the name of our project, activate the Add => Resource Dictionary item ... and in the dialog box that appears, select the element of the same name:



5. Through the context menu of the project, we add a new “Class” file to it, assigning the name “MyConverters.cs” to it.
6. We connect the assembly “System.Data.Linq” to our project.
7. In the file created in paragraph 5 we write the following code:

using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  1. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  2. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  3. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  4. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  5. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  6. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  7. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  8. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  9. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  10. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  11. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  12. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  13. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  14. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  15. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  16. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  17. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  18. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  19. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  20. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  21. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  22. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  23. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  24. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  25. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  26. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  27. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  28. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  29. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  30. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  31. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  32. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  33. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  34. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  35. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  36. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  37. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  38. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  39. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  40. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  41. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  42. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  43. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  44. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  45. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  46. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  47. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  48. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  49. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  50. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
  51. using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .
using System; using System.Collections. Generic ; using System.Linq; using System.Text; using System.Windows.Data; using Linq2Sql; using System.Xml.Linq; namespace WpfGuiProject { // Category. - Category. public sealed class CategoryValueConverter : IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is Category) { Category x = (Category) value ; return (x).Categories.Where(n => n.ParrentCategoryId == x.CategoryId).OrderBy(n => n.CategoryName); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } // XElement. - XElement, // Category. public sealed class XmlConverter :IValueConverter { public object Convert ( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ( value is XElement ) { XElement x = ( XElement ) value ; return x.Elements( "Category" ); } else { return null ; } } public object ConvertBack( object value , Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } * This source code was highlighted with Source Code Highlighter .

Created in paragraph 7 is the second (of the three) key point for solving the task before us - a hierarchical visual presentation of information, implemented with the help of data binding. In the code, we define the logic for obtaining the children we need, which should be involved in building a hierarchical model.
8. Edit the file "Dictionary1.xaml":

  1. < ResourceDictionary xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns: linq2Xml = "clr-namespace: System.Xml.Linq; assembly = System.Xml.Linq"
  4. xmlns: linq = "clr-namespace: Linq2Sql; assembly = Linq2Sql"
  5. xmlns: local = "clr-namespace: WpfGuiProject" >
  6. <! - Template for hierarchical display of instances of the TreeTable class IN THE CASE OF CONNECTING TO THE DATABASE ->
  7. < HierarchicalDataTemplate x: Key = "key1" DataType = "{x: Type linq: Category}" >
  8. <! - I specify the data source on the basis of which the partition tree should be formed ->
  9. < HierarchicalDataTemplate.ItemsSource >
  10. < Binding Path = "." >
  11. <! - I specify a converter that allows you to get a list of child elements of type Category, in relation to this ->
  12. < Binding.Converter >
  13. < local: CategoryValueConverter />
  14. </ Binding.Converter >
  15. </ Binding >
  16. </ HierarchicalDataTemplate.ItemsSource >
  17. <! - I create a visual representation of the element displayed in the section tree ->
  18. < DockPanel >
  19. < TextBlock Text = "{Binding Path = CategoryName}" >
  20. < TextBlock.ToolTip >
  21. < Binding Path = "Description" />
  22. </ TextBlock.ToolTip >
  23. </ Textblock >
  24. <! - Number of books placed directly in the section ->
  25. < TextBlock Name = "txtLeft" Text = "(" Grid . Column = "1" />
  26. < TextBlock Name = "txtCount" Text = "{Binding Path = Books.Count}" Grid . Column = "2" />
  27. < TextBlock Name = "txtRight" Text = ")" Grid . Column = "3" />
  28. </ DockPanel >
  29. </ HierarchicalDataTemplate >
  30. <! - Template for displaying instances of the class Book in the event of connecting to the database ->
  31. < DataTemplate DataType = "{x: Type linq: Book}" >
  32. < TextBlock Text = "{Binding Path = BookName}" >
  33. < TextBlock.ToolTip >
  34. < Binding Path = "ToolTipText" />
  35. </ TextBlock.ToolTip >
  36. </ Textblock >
  37. </ DataTemplate >
  38. <! - Template for hierarchical data display in case of connecting to XML-file ->
  39. < HierarchicalDataTemplate x: Key = "key2" DataType = "{x: Type linq2Xml: XElement}" >
  40. <! - I specify the data source on the basis of which the partition tree should be formed ->
  41. < HierarchicalDataTemplate.ItemsSource >
  42. < Binding Path = "." >
  43. <! - I specify a converter that allows you to get a list of child elements of type Category, in relation to this ->
  44. < Binding.Converter >
  45. < local: XmlConverter />
  46. </ Binding.Converter >
  47. </ Binding >
  48. </ HierarchicalDataTemplate.ItemsSource >
  49. <! - I create a visual representation of the element displayed in the section tree ->
  50. < TextBlock Text = "{Binding Path = Attribute [CategoryName] .Value}" >
  51. < TextBlock.ToolTip >
  52. < Binding Path = "Attribute [ToolTipText] .Value" />
  53. </ TextBlock.ToolTip >
  54. </ Textblock >
  55. </ HierarchicalDataTemplate >
  56. <! - Template for displaying instances of the class Book in the event of connecting to an XML file ->
  57. < DataTemplate x: Key = "key3" DataType = "{x: Type linq2Xml: XElement}" >
  58. < Grid >
  59. < Grid.RowDefinitions >
  60. < RowDefinition />
  61. < RowDefinition Height = "Auto" />
  62. </ Grid.RowDefinitions >
  63. < TextBlock Text = "{Binding Path = Attribute [BookName] .Value}" >
  64. < TextBlock.ToolTip >
  65. < Binding Path = "Attribute [ToolTipText] .Value" />
  66. </ TextBlock.ToolTip >
  67. </ Textblock >
  68. </ Grid >
  69. </ DataTemplate >
  70. </ ResourceDictionary >
* This source code was highlighted with Source Code Highlighter .


The xml markup created in step 8 is the third key point. In this markup, using the HierarchicalDataTemplate class is defined — a data display template and a converter that should be used to get children relative to the current one.

Now we are starting to form a graphical representation of our window.

9. Edit the contents of the file "MainWindow.xaml":

  1. < Window x: Class = "WpfGuiProject.MainWindow"
  2. xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns: linq = "clr-namespace: Linq2Sql; assembly = Linq2Sql"
  5. xmlns: local = "clr-namespace: WpfGuiProject"
  6. Title = "Hierarchical display of data using the binding" Height = "350" Width = "525" >
  7. <! - Connect the resource file ->
  8. < Window.Resources >
  9. < ResourceDictionary >
  10. < ResourceDictionary.MergedDictionaries >
  11. < ResourceDictionary Source = "Dictionary1.xaml" />
  12. </ ResourceDictionary.MergedDictionaries >
  13. </ ResourceDictionary >
  14. </ Window.Resources >
  15. <! - We create the visual contents of the window ->
  16. < Grid >
  17. < Grid.ColumnDefinitions >
  18. < ColumnDefinition Width = "50 *" />
  19. < ColumnDefinition Width = "Auto" />
  20. < ColumnDefinition Width = "50 *" />
  21. </ Grid.ColumnDefinitions >
  22. < Grid.RowDefinitions >
  23. < RowDefinition Height = "Auto" />
  24. < RowDefinition Height = "100 *" />
  25. < RowDefinition Height = "Auto" />
  26. </ Grid.RowDefinitions >
  27. <! - Display the tree structure ->
  28. < TreeView Name = "treeStructure" Margin = "2" Grid . Row = "1" />
  29. <! - Display notes for the item selected in the hierarchy ->
  30. < GroupBox Header = "Note" Margin = "2" Grid . Row = "2" >
  31. < TextBlock Name = "selectedNodeDescription" TextWrapping = "Wrap" Text = "{Binding ElementName = treeStructure, Path = SelectedItem.Description}" />
  32. </ Groupbox >
  33. < GridSplitter Width = "5" HorizontalAlignment = "Center" VerticalAlignment = "Stretch" Grid . Column = "1" Grid . RowSpan = "3" />
  34. < ListBox Name = "listBooks" Margin = "2" Grid . Column = "2" Grid . RowSpan = "2" ItemsSource = "{Binding ElementName = treeStructure, Path = SelectedItem.Books}" />
  35. < GroupBox Header = "Note" Margin = "2" Grid . Column = "2" Grid . Row = "2" >
  36. < TextBlock Name = "selectedBookDescription" TextWrapping = "Wrap" Text = "{Binding ElementName = listBooks, Path = SelectedItem.Description}" />
  37. </ Groupbox >
  38. <! - Provide the user to select the data source of interest. This block must be placed AFTER the XAML markup, which defines
  39. treeStructure element. This requirement is due to the fact that the XAML markup of the RadioButton elements contains the registration
  40. Checked events, the body of which, in turn, contains code using the treeStructure element, and according to the rules of XAML documents, the element cannot be used
  41. before it is determined ->
  42. < GroupBox Header = "Data Source" Margin = "2" >
  43. < StackPanel Margin = "2" >
  44. < RadioButton Name = "rbDatabase" Content = "MS SQL Server Database " IsChecked = "True" Checked = "Change_DataSource" />
  45. < RadioButton Name = "rbXmlFile" Content = "XML file" IsChecked = "False" Checked = "Change_DataSource" />
  46. </ StackPanel >
  47. </ Groupbox >
  48. </ Grid >
  49. </ Window >
* This source code was highlighted with Source Code Highlighter .


This markup will form the following window:



10. Add the file “XMLFile1.xml” to our project, which will be an alternative data source:



Fill the file we created with the data written in the xml-format:

  1. <? xml version = "1.0" encoding = "utf-8" ? >
  2. < MyXmlDocument >
  3. < Category CategoryName = "Computer literature" Description = "Various computer literature" ToolTipText = "My section" >
  4. < Category CategoryName = "Windows applications" Description = "This OS is most popular" ToolTipText = "My axis" >
  5. < Category CategoryName = "MS Office 2007" Description = "Office Suite" ToolTipText = "My Office" />
  6. < Category CategoryName = "Autodesk Products" Description = "Various CAD" ToolTipText = "My Choice" >
  7. < Category CategoryName = "AutoCAD" Description = "Most Common CAD" ToolTipText = "My CAD" >
  8. < Book BookName = "AutoCAD 2010. N.N. Poleshchuk" Description = "My book for this CAD" ToolTipText = "Reference" />
  9. < Book BookName = "AutoCAD 2007, user's bible. (I don’t remember the author)" Description = "Another my book on this CAD system" ToolTipText = "Old book" />
  10. </ Category >
  11. < Category CategoryName = "Revit" Description = "new generation of CAD (BIM)" ToolTipText = "Not yet my CAD" />
  12. </ Category >
  13. </ Category >
  14. < Category CategoryName = "Linux applications" Description = "Free OS" ToolTipText = "Interesting, but not very common" />
  15. </ Category >
  16. < Category CategoryName = "Classic" Description = "Classic Literature" ToolTipText = "Useful for General Development" >
  17. < Category CategoryName = "Stories and Stories" Description = "Verses by Russian Authors" ToolTipText = "For the Soul" >
  18. < Book BookName = "Captain's daughter. AS Pushkin" Description = "School course" ToolTipText = "I read once ..." />
  19. </ Category >
  20. < Category CategoryName = "Poetry" Description = "Stories and stories of domestic authors" ToolTipText = "This is also for the soul" />
  21. </ Category >
  22. </ MyXmlDocument >
* This source code was highlighted with Source Code Highlighter .


11. Make changes to the “MainWindow.xaml.cs” file:

  1. using System;
  2. using System.Collections. Generic ;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. // Add links to the namespaces we need
  15. using Linq2Sql;
  16. using System.Xml.Linq;
  17. namespace WpfGuiProject
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </ summary>
  22. public partial class MainWindow: Window
  23. {
  24. XElement xml;
  25. public MainWindow ()
  26. {
  27. Initializecomponent ();
  28. xml = XElement .Load ( @ ".. \ .. \ XMLFile1.xml" );
  29. }
  30. // This method is all the logic for specifying the data source to be displayed in the window
  31. private void Change_DataSource ( object sender, RoutedEventArgs e)
  32. {
  33. if (rbDatabase.IsChecked == true ) // Database is selected as a data source
  34. {
  35. listBooks.ItemTemplate = null ;
  36. treeStructure.ItemsSource = new MyTestDbDataContext (). Categories.Where (n => n.ParrentCategoryId == null );
  37. treeStructure.ItemTemplate = (HierarchicalDataTemplate) FindResource ( "key1" );
  38. // Customize Note Binding
  39. DescriptionBinding (selectedNodeDescription, "SelectedItem.Description" , treeStructure);
  40. DescriptionBinding (selectedBookDescription, "SelectedItem.Description" , listBooks);
  41. // Set up the book display binding
  42. Binding bind = new Binding ( "SelectedItem.Books" ) {Source = treeStructure};
  43. listBooks.SetBinding (ItemsControl.ItemsSourceProperty, bind);
  44. }
  45. else // XML file is selected as a data source
  46. {
  47. treeStructure.ItemsSource = xml.Elements ( "Category" );
  48. treeStructure.ItemTemplate = (HierarchicalDataTemplate) FindResource ( "key2" );
  49. // Customize Note Binding
  50. DescriptionBinding (selectedNodeDescription, "SelectedItem.Attribute [Description] .Value" , treeStructure);
  51. DescriptionBinding (selectedBookDescription, "SelectedItem.Attribute [Description] .Value" , listBooks);
  52. // Set up the book display binding
  53. listBooks.ItemTemplate = (DataTemplate) FindResource ( "key3" );
  54. Binding bind = new Binding ( "SelectedItem.Elements [Book]" ) {Source = treeStructure};
  55. listBooks.SetBinding (ItemsControl.ItemsSourceProperty, bind);
  56. }
  57. }
  58. /// <summary>
  59. /// Configure data mapping binding
  60. /// </ summary>
  61. /// <param name = "textBlock"> A text object that should display the text of the note </ param>
  62. /// <param name = "pathValue"> Binding Path value </ param>
  63. /// <param name = "source"> link to the source object from which data is read through the Path property </ param>
  64. void DescriptionBinding (TextBlock textBlock, string pathValue, Control source)
  65. {
  66. textBlock.SetBinding (TextBlock. TextProperty, new Binding (pathValue) {Source = source});
  67. }
  68. }
  69. }
* This source code was highlighted with Source Code Highlighter .


Now you can run our application for execution and enjoy the results of work ... So, press the F5 key and see what we have ...

A. The source is the database:



B. The source is an xml file:



Solution source code in MS Visual Studio 2010 format can be downloaded here .

Source: https://habr.com/ru/post/94231/


All Articles