<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;D0YBQHg9fyp7ImA9WhBbFkk.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345</id><updated>2013-05-15T22:12:31.667+03:00</updated><category term="XamDataGrid" /><category term="Visual Studio" /><category term="Extended WPF Toolkit" /><category term="MVVM" /><category term="Performance" /><category term="SQL" /><category term="IDataErrorInfo" /><category term="Dependency Injection" /><category term="DataGrid" /><category term="String" /><category term="DataTable" /><category term="ScrollViewer" /><category term="SortMemberPath" /><category term="XAML" /><category term="Infragistics" /><category term="Configuration Settings" /><category term="XLINQ" /><category term="App.Config" /><category term="Windows Service" /><category term="LINQ to SQL" /><category term="Events" /><category term="AutoMapper" /><category term="Expander" /><category term="WinForms" /><category term="Design Patterns" /><category term="WPF Animation" /><category term="Factory" /><category term="RowDetailsTemplate" /><category term="Menu" /><category term="LINQ" /><category term="DateTimePicker" /><category term="DataView" /><category term="XSLT" /><category term="ObjectDataProvider" /><category term="Async" /><category term="Await" /><category term="DataGridRow" /><category term="Logging" /><category term="ToggleButton" /><category term="XML" /><category term="Recursion" /><category term="WPF Styles" /><category term="Source Code" /><category term="C#" /><category term="Regex" /><category term="Reflection" /><category term="WCF" /><category term="DataGridTemplateColumn" /><category term="Polymorphism" /><category term="Validation" /><category term="Singleton" /><category term="Autofac" /><category term="Job Interview" /><category term="Multithreading" /><category term="Enum" /><category term="WPF" /><category term="Task" /><category term="Entity Framework" /><title>Code Arsenal - .NET Tutorials and Code Samples</title><subtitle type="html">CodeArsenal.Net - Tutorials and Code Samples for .NET Developers.
This site focuses on C# language and such .NET technologies as WPF, XAML, LINQ, WCF, Web Services, XML.
You can Download Example's Source Code.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://www.codearsenal.net/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://www.codearsenal.net/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>48</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/CodeArsenal" /><feedburner:info uri="codearsenal" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>CodeArsenal</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><entry gd:etag="W/&quot;CEUBQn4zfSp7ImA9WhBVE0Q.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-8955691306476831635</id><published>2013-04-19T20:16:00.001+03:00</published><updated>2013-04-19T20:24:13.085+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-04-19T20:24:13.085+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Singleton" /><title>Singleton Design Pattern C# Example</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
&lt;a href="http://www.codearsenal.net/search/label/Singleton"&gt;Singleton&lt;/a&gt; is one of the most frequently used &lt;a href="http://www.codearsenal.net/search/label/Design%20Patterns"&gt;Design Patterns&lt;/a&gt; in Software Engineering. You should apply Singleton pattern if you want to ensure that your class will have only one instance with a global point of access to it.&lt;/div&gt;
&lt;br /&gt;
Let's look at Singleton code:
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Singleton
{
   &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Singleton instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; Singleton();
   
   &lt;span class="kwrd"&gt;private&lt;/span&gt; Singleton(){}

   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton GetInstance
   {
      &lt;span class="kwrd"&gt;get&lt;/span&gt; 
      {
         &lt;span class="kwrd"&gt;return&lt;/span&gt; instance; 
      }
   }
}&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
As you can see the code is pretty simple. Class is defined as &lt;i&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;sealed&lt;/span&gt;&lt;/i&gt; in order to prevent derivation. Also Singleton variable is marked &lt;i&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;readonly&lt;/span&gt;&lt;/i&gt;, which means that it can be assigned only during static initialization or in a constructor.
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
This approach has only one downside - it's not thread safe. This code must be improved if you work in multithreaded environment.&lt;/div&gt;
&lt;br/&gt;&lt;a name='more'&gt;&lt;/a&gt;
&lt;div style="text-align: justify;"&gt;
Here is a thread safe version of Singleton:
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Singleton
{
   &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;volatile&lt;/span&gt; Singleton instance;
   &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; sync = &lt;span class="kwrd"&gt;new&lt;/span&gt; Object();

   &lt;span class="kwrd"&gt;private&lt;/span&gt; Singleton() {}

   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton GetInstance
   {
      &lt;span class="kwrd"&gt;get&lt;/span&gt; 
      {
         &lt;span class="kwrd"&gt;if&lt;/span&gt; (instance == &lt;span class="kwrd"&gt;null&lt;/span&gt;) 
         {
            &lt;span class="kwrd"&gt;lock&lt;/span&gt; (sync) 
            {
               &lt;span class="kwrd"&gt;if&lt;/span&gt; (instance == &lt;span class="kwrd"&gt;null&lt;/span&gt;) 
                  instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; Singleton();
            }
         }

         &lt;span class="kwrd"&gt;return&lt;/span&gt; instance;
      }
   }
}&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
In this version of Singleton we resolved thread concurrency problems, so you can use it in multithreaded environment.&lt;/div&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/96xtHR8eUsw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/8955691306476831635/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2013/04/singleton-design-pattern-csharp-example.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8955691306476831635?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8955691306476831635?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/96xtHR8eUsw/singleton-design-pattern-csharp-example.html" title="Singleton Design Pattern C# Example" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2013/04/singleton-design-pattern-csharp-example.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0EDRXw-cCp7ImA9WhBWGU8.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-6825381331347691449</id><published>2013-04-06T16:26:00.002+03:00</published><updated>2013-04-14T10:47:54.258+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-04-14T10:47:54.258+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Factory" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><title>Factory Design Pattern C# Example</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
&lt;a href="http://www.codearsenal.net/search/label/Factory" target="_blank"&gt;Factory&lt;/a&gt; it's such a &lt;a href="http://www.codearsenal.net/search/label/Design%20Patterns" target="_blank"&gt;Design Pattern&lt;/a&gt; which defines an interface for creating an object, but lets the classes that implement the interface decide which class to instantiate. Factory Pattern lets a class postpone instantiation to sub-classes.
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Let's see Factory Pattern in action on a simple example. Assume you have two different classes &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Toyota&lt;/span&gt; and &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Suzuki&lt;/span&gt;, they both implements abstract class Car. You need to instantiate one of these classes, but you don't know which of them, it depends on user. This is perfect scenario for the Factory Pattern.&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-2DSvLN2rl2k/UWASHoHmeNI/AAAAAAAAEQQ/sEpxM5GlOFA/s1600/Factory.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Factory Design Pattern example program diagram" border="0" height="177" src="http://3.bp.blogspot.com/-2DSvLN2rl2k/UWASHoHmeNI/AAAAAAAAEQQ/sEpxM5GlOFA/s400/Factory.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;
So here are our two main classes and the interface (abstract class):
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Car
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Company { &lt;span class="kwrd"&gt;get&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Model { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Speed { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
}

&lt;span class="kwrd"&gt;class&lt;/span&gt; Suzuki : Car
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _company;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _model;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; _speed;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Suzuki(&lt;span class="kwrd"&gt;string&lt;/span&gt; model, &lt;span class="kwrd"&gt;int&lt;/span&gt; speed)
    {
        _company = &lt;span class="str"&gt;"Suzuki"&lt;/span&gt;;
        _model = model;
        _speed = speed;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Company
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; _company; }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Model
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; _model; }
        &lt;span class="kwrd"&gt;set&lt;/span&gt; { _model = value; }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Speed
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; _speed; }
        &lt;span class="kwrd"&gt;set&lt;/span&gt; { _speed = value; }
    }
}

&lt;span class="kwrd"&gt;class&lt;/span&gt; Toyota : Car
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _company;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _model;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; _speed;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Toyota(&lt;span class="kwrd"&gt;string&lt;/span&gt; model, &lt;span class="kwrd"&gt;int&lt;/span&gt; speed)
    {
        _company = &lt;span class="str"&gt;"Toyota"&lt;/span&gt;;
        _model = model;
        _speed = speed;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Company
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; _company; }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Model
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; _model; }
        &lt;span class="kwrd"&gt;set&lt;/span&gt; { _model = value; }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Speed
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; _speed; }
        &lt;span class="kwrd"&gt;set&lt;/span&gt; { _speed = value; }
    }
}&lt;/pre&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Middle */
google_ad_slot = "2969384752";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
Abstract &lt;span style="font-family: Courier New, Courier, monospace;"&gt;CarFactory&lt;/span&gt; class is an interface for our &lt;span style="font-family: Courier New, Courier, monospace;"&gt;ToyotaFactory&lt;/span&gt; and &lt;span style="font-family: Courier New, Courier, monospace;"&gt;SuzukiFactory&lt;/span&gt; classes that will be responsible for the instantiation of corresponding car, therefore they only have one single &lt;span style="font-family: Courier New, Courier, monospace;"&gt;GetCar()&lt;/span&gt; method. 
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CarFactory
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; Car GetCar();
}

&lt;span class="kwrd"&gt;class&lt;/span&gt; ToyotaFactory : CarFactory
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _model;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; _speed;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; ToyotaFactory(&lt;span class="kwrd"&gt;string&lt;/span&gt; model, &lt;span class="kwrd"&gt;int&lt;/span&gt; speed)
    {
        _model = model;
        _speed = speed;
    }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Car GetCar()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Toyota(_model, _speed);
    }
}

&lt;span class="kwrd"&gt;class&lt;/span&gt; SuzukiFactory: CarFactory
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _model;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; _speed;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; SuzukiFactory(&lt;span class="kwrd"&gt;string&lt;/span&gt; model, &lt;span class="kwrd"&gt;int&lt;/span&gt; speed)
    {
        _model = model;
        _speed = speed;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Car GetCar()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Suzuki(_model, _speed);
    }
}&lt;/pre&gt;
What is left is only &lt;span style="font-family: Courier New, Courier, monospace;"&gt;CarAssembler&lt;/span&gt; class which will create needed type of car depending on the factory that it receives as parameter.
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; CarAssembler
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AssembleCar(CarFactory factory)
    {
        Car car = factory.GetCar();
        Console.WriteLine(&lt;span class="str"&gt;"{0} {1} assembled successfully. Maximum speed is {2} mph"&lt;/span&gt;, 
            car.Company, car.Model, car.Speed);
    }
}&lt;/pre&gt;
That's all! Now you can instantiate corresponding car object depending on user input:
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
{
    CarFactory factory = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
    Console.WriteLine(&lt;span class="str"&gt;"Which car you would like to build?"&lt;/span&gt;);
    &lt;span class="kwrd"&gt;string&lt;/span&gt; car = Console.ReadLine();
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (car == &lt;span class="str"&gt;"toyota"&lt;/span&gt;)
        factory = &lt;span class="kwrd"&gt;new&lt;/span&gt; ToyotaFactory(&lt;span class="str"&gt;"Corolla"&lt;/span&gt;, 130);
    &lt;span class="kwrd"&gt;else&lt;/span&gt;
        factory = &lt;span class="kwrd"&gt;new&lt;/span&gt; SuzukiFactory(&lt;span class="str"&gt;"Swift"&lt;/span&gt;, 110);

    &lt;span class="kwrd"&gt;new&lt;/span&gt; CarAssembler().AssembleCar(factory);
}&lt;/pre&gt;
Here is the program output:

&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-MJkTaQE326A/UWAhFA4ksxI/AAAAAAAAEQg/0lMIIkefCjc/s1600/Factory-example-output.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Factory Pattern example output" border="0" height="111" src="http://2.bp.blogspot.com/-MJkTaQE326A/UWAhFA4ksxI/AAAAAAAAEQg/0lMIIkefCjc/s400/Factory-example-output.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://www.mediafire.com/download.php?iinbrz7o9zr71k5" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2012 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/gc9Tielhgy0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/6825381331347691449/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2013/04/csharp-design-patterns-factory.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/6825381331347691449?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/6825381331347691449?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/gc9Tielhgy0/csharp-design-patterns-factory.html" title="Factory Design Pattern C# Example" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-2DSvLN2rl2k/UWASHoHmeNI/AAAAAAAAEQQ/sEpxM5GlOFA/s72-c/Factory.JPG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2013/04/csharp-design-patterns-factory.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0YCSH87fyp7ImA9WhBWGU8.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-7019901653091918110</id><published>2013-03-31T15:28:00.002+03:00</published><updated>2013-04-14T11:46:09.107+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-04-14T11:46:09.107+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Autofac" /><category scheme="http://www.blogger.com/atom/ns#" term="Dependency Injection" /><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><title>Managing Dependency Injection in C# with Autofac</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;a href="http://www.codearsenal.net/search/label/Dependency%20Injection" target="_blank"&gt;Dependency Injection&lt;/a&gt; in software engineering is a specific form of &lt;i&gt;invasion of control&lt;/i&gt;. Conventionally, when an object needs access to a particular service, this object takes responsibility for access to this service. Commonly, an object receives a link to the service location or appeals to some "service locator" and requests a reference to the implementation of a specific type of service. When implementing Dependency Injection, object simply provides a property that is able to keep a reference to the type of service, and when the object is created, a reference to the implementation of the desired type of service is automatically inserted into the property (field). Dependency Injection is more flexible because it becomes easier to create alternative implementations of this type of service, and then specify exactly which implementation should be used, for example in the configuration file, with no changes in the objects that use this service. This is especially useful in unit testing.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-yjxgZUuyssA/UVWyv_8-jEI/AAAAAAAAEL4/Iq7TdaYfk2E/s1600/autofac.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="C# Dependency Injection with Autofac" border="0" height="50" src="http://1.bp.blogspot.com/-yjxgZUuyssA/UVWyv_8-jEI/AAAAAAAAEL4/Iq7TdaYfk2E/s200/autofac.JPG" title="" width="200" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Today I'll show a simple example of managing dependency injection with &lt;a href="http://www.codearsenal.net/search/label/Autofac" target="_blank"&gt;Autofac&lt;/a&gt;. Autofac is an &lt;i&gt;Inversion of Control Container&lt;/i&gt; for .NET. It helps to manage dependencies between classes so that applications stay easy to change as they grow in size and complexity. This achieved by treating regular .NET classes as &lt;i&gt;components&lt;/i&gt;. You can download Autofac from &lt;a href="https://code.google.com/p/autofac/" target="_blank"&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Our example application is a console program that checks a list of employees and notifies which of them are experts (work experience more than 10 years).&lt;br /&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;Here is our &lt;i&gt;Employee &lt;/i&gt;class:&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;


&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Employee
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Position { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Experience { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
}&lt;/pre&gt;
&lt;i&gt;EmployeeObserver &lt;/i&gt;class is the core of the application, its main purpose is to find employees that satisfy our condition - their work&amp;nbsp;experiences is more than ten years.
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; EmployeeObserver
{
    &lt;span class="kwrd"&gt;readonly&lt;/span&gt; IList&amp;lt;Employee&amp;gt; _employees;
    &lt;span class="kwrd"&gt;readonly&lt;/span&gt; IEmployeeNotifier _notifier;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; EmployeeObserver(IList&amp;lt;Employee&amp;gt; employees, IEmployeeNotifier notifier)
    {
        _employees = employees;
        _notifier = notifier;
    }

    &lt;span class="rem"&gt;// find employees with more than 10 years of experience&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; FindExperts()
    {
        &lt;span class="kwrd"&gt;var&lt;/span&gt; experts = _employees.Where(e =&amp;gt; e.Experience &amp;gt; 10);

        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;var&lt;/span&gt; e &lt;span class="kwrd"&gt;in&lt;/span&gt; experts)
            _notifier.PrintExperts(e);
    }
} &lt;/pre&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Middle */
google_ad_slot = "2969384752";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
Now we need to create a &lt;i&gt;Printer &lt;/i&gt;class which will be responsible for printing out messages to a &lt;i&gt;TextStream&lt;/i&gt;. &lt;i&gt;Printer &lt;/i&gt;will implement &lt;i&gt;IPrinter&lt;/i&gt; interface.
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;interface&lt;/span&gt; IPrinter
{
    &lt;span class="kwrd"&gt;void&lt;/span&gt; PrintExperts(Employee employee);
}

&lt;span class="kwrd"&gt;class&lt;/span&gt; Printer : IPrinter
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; TextWriter _textWriter;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Printer(TextWriter writer)
    {
        _textWriter = writer;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PrintExperts(Employee employee)
    {
        _textWriter.WriteLine(&lt;span class="str"&gt;"Employee {0} is an expert."&lt;/span&gt;, employee.Name);            
    }
}&lt;/pre&gt;
Now all set for Dependency Injection. As you can see we can make Dependency Injection by hand, without Autofac:
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; di = &lt;span class="kwrd"&gt;new&lt;/span&gt; EmployeeObserver(employees, &lt;span class="kwrd"&gt;new&lt;/span&gt; Printer(Console.Out));
di.FindExperts();&lt;/pre&gt;
In fact this code looks very simple, but in real application with many layers and all kind of components configuring dependency injection by hand will become very complex. Dependency injection containers, such as Autofac, bring order to this world of chaos. Let's see how we use it:
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;ContainerBuilder autofac = &lt;span class="kwrd"&gt;new&lt;/span&gt; ContainerBuilder();
autofac.Register(o =&amp;gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; EmployeeObserver(o.Resolve&amp;lt;IList&amp;lt;Employee&amp;gt;&amp;gt;(), o.Resolve&amp;lt;IPrinter&amp;gt;()));
autofac.RegisterType&amp;lt;Printer&amp;gt;().As&amp;lt;IPrinter&amp;gt;();
autofac.RegisterInstance(employees).As&amp;lt;IList&amp;lt;Employee&amp;gt;&amp;gt;();
autofac.RegisterInstance(Console.Out).As&amp;lt;TextWriter&amp;gt;();
&lt;span class="kwrd"&gt;using&lt;/span&gt; (&lt;span class="kwrd"&gt;var&lt;/span&gt; container = autofac.Build())
{
    container.Resolve&amp;lt;EmployeeObserver&amp;gt;().FindExperts();
}&lt;/pre&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-1FSQfhX_rtg/UVgmZEnQeMI/AAAAAAAAEME/O8V4EHryBco/s1600/autofac_output.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Autofac application output" border="0" height="141" src="http://1.bp.blogspot.com/-1FSQfhX_rtg/UVgmZEnQeMI/AAAAAAAAEME/O8V4EHryBco/s320/autofac_output.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
May be on this particular example it's hard to see why this approach is better than configuring dependency injection by hand, but you should notice one important thing - with Autofac each component is configured independently of all the others, and this is what will make a big difference when your application become more complex.
&lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://www.mediafire.com/download.php?3t6249r568b6dzt" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2012 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/4xVVYYJOUko" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/7019901653091918110/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2013/03/dependency-injection-in-csharp-autofac.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/7019901653091918110?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/7019901653091918110?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/4xVVYYJOUko/dependency-injection-in-csharp-autofac.html" title="Managing Dependency Injection in C# with Autofac" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-yjxgZUuyssA/UVWyv_8-jEI/AAAAAAAAEL4/Iq7TdaYfk2E/s72-c/autofac.JPG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2013/03/dependency-injection-in-csharp-autofac.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkEDQHk6eyp7ImA9WhNVFE4.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-4008394047225864836</id><published>2012-12-24T17:56:00.002+02:00</published><updated>2012-12-25T13:24:31.713+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-12-25T13:24:31.713+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="AutoMapper" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGrid" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>C# Object-to-Object Mapping - AutoMapper Example</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
Imaging this scenario: We have an Employee object with many properties in it, and we want to display our Employee in a &lt;span style="font-family: Courier New, Courier, monospace;"&gt;DataGrid&lt;/span&gt; or some other control. For this purpose we need a lighter version of Employee, let's call it &lt;span style="font-family: Courier New, Courier, monospace;"&gt;EmployeeViewItem&lt;/span&gt;. A list of &lt;span style="font-family: Courier New, Courier, monospace;"&gt;EmployeeViewItem&lt;/span&gt; will need to bind to our &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;a href="http://www.codearsenal.net/search/label/DataGrid"&gt;DataGrid&lt;/a&gt;&lt;/span&gt;.
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Employee
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Email { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Address Address { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Position { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; Gender { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Age { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }            
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; YearsInCompany { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime StartDate { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Address
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Country { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; City { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Street { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Number { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }            
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EmployeeViewItem
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Email { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Address { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Position { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Gender { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Age { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }            
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; YearsInCompany { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; StartDate { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
}&lt;/pre&gt;
Ok, so what we usually do? Something like this, right?
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;EmployeeViewItem viewItem = &lt;span class="kwrd"&gt;new&lt;/span&gt; EmployeeViewItem();
viewItem.Name = employee.Name;
viewItem.Email = employee.Email;
viewItem.Address = employee.Address.City + employee.Address.Street + employee.Address.Number;
viewItem.Position = employee.Position;
viewItem.Gender = employee.Gender == &lt;span class="kwrd"&gt;true&lt;/span&gt; ? &lt;span class="str"&gt;"Man"&lt;/span&gt; : &lt;span class="str"&gt;"Female"&lt;/span&gt;;
viewItem.Age = employee.Age;
viewItem.YearsInCompany = employee.YearsInCompany;
viewItem.StartDate = employee.StartDate.ToShortDateString();&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
Quite annoying, isn't it? What if we could do this mapping automatically in some way? I hasten to please you - yes, you can map these two object automatically with &lt;b&gt;AutoMapper&lt;/b&gt;!
As written on AutoMapper &lt;a href="http://automapper.org/" target="_blank"&gt;webpage &lt;/a&gt;- "AutoMapper is a simple little library built to solve a deceptively complex problem - getting rid of code that mapped one object to another."&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;
&lt;div style="text-align: justify;"&gt;
I'll show how to use AutoMapper on our &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Employee&lt;/span&gt; to &lt;span style="font-family: Courier New, Courier, monospace;"&gt;EmployeeViewItem&lt;/span&gt; mapping example.&lt;br /&gt;
First, download AutoMapper from &lt;a href="https://github.com/AutoMapper/AutoMapper" target="_blank"&gt;here&lt;/a&gt;. It's a standalone assembly, so you'll need to add reference to it in your project.&lt;br /&gt;
&lt;br /&gt;
In order to tell to AutoMapper what exactly to map we need to write this line of code:
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;Mapper.CreateMap&amp;lt;Employee, EmployeeViewItem&amp;gt;();&lt;/pre&gt;
And to actually perform the mapping we write this thing:
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;EmployeeViewItem employeeViewItem = Mapper.Map&amp;lt;Employee, Employee&amp;gt;(employee);&lt;/pre&gt;
Where &lt;span style="font-family: Courier New, Courier, monospace;"&gt;employee&lt;/span&gt; is an object of &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Employee&lt;/span&gt; class. This is it!&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
But, as you probably noticed, our &lt;span style="font-family: Courier New, Courier, monospace;"&gt;EmployeeViewItem&lt;/span&gt; class has several inconsistencies with Employee class. Address is a complex object in Employee, but a simple string in &lt;span style="font-family: Courier New, Courier, monospace;"&gt;EmployeeViewItem&lt;/span&gt;. &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Gender&lt;/span&gt; is boolean in &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Employee&lt;/span&gt;, but is a string in &lt;span style="font-family: Courier New, Courier, monospace;"&gt;EmployeeViewItem&lt;/span&gt;. &lt;span style="font-family: Courier New, Courier, monospace;"&gt;StartDate&lt;/span&gt; is a &lt;span style="font-family: Courier New, Courier, monospace;"&gt;DateTime&lt;/span&gt; in &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Employee&lt;/span&gt;, but a string in &lt;span style="font-family: Courier New, Courier, monospace;"&gt;EmployeeViewItem&lt;/span&gt;. So, we need to do some tweaks to CreateMap method in Mapper. The final code will look like this:&lt;/div&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Middle */
google_ad_slot = "2969384752";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Program
{
    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
    {
        Mapper.CreateMap&amp;lt;Employee, EmployeeViewItem&amp;gt;()
           .ForMember(ev =&amp;gt; ev.Address, 
                       m =&amp;gt; m.MapFrom(a =&amp;gt; a.Address.City + &lt;span class="str"&gt;", "&lt;/span&gt; + 
                                           a.Address.Street + &lt;span class="str"&gt;" "&lt;/span&gt; + 
                                           a.Address.Number)
                      )
           .ForMember(ev =&amp;gt; ev.Gender, 
                       m =&amp;gt; m.ResolveUsing&amp;lt;GenderResolver&amp;gt;().FromMember(e =&amp;gt; e.Gender))
           .ForMember(ev =&amp;gt; ev.StartDate, m =&amp;gt; m.AddFormatter&amp;lt;DateFormatter&amp;gt;());

        Employee employee = &lt;span class="kwrd"&gt;new&lt;/span&gt; Employee
        {
            Name = &lt;span class="str"&gt;"John SMith"&lt;/span&gt;,
            Email = &lt;span class="str"&gt;"john@codearsenal.net"&lt;/span&gt;,
            Address = &lt;span class="kwrd"&gt;new&lt;/span&gt; Address
            {
                Country = &lt;span class="str"&gt;"USA"&lt;/span&gt;,
                City = &lt;span class="str"&gt;"New York"&lt;/span&gt;,
                Street = &lt;span class="str"&gt;"Wall Street"&lt;/span&gt;,
                Number = 7
            },
            Position = &lt;span class="str"&gt;"Manager"&lt;/span&gt;,
            Gender = &lt;span class="kwrd"&gt;true&lt;/span&gt;,
            Age = 35,                
            YearsInCompany = 5,
            StartDate = &lt;span class="kwrd"&gt;new&lt;/span&gt; DateTime(2007, 11, 2)
        };
        
        EmployeeViewItem employeeVIewItem = Mapper.Map&amp;lt;Employee, EmployeeViewItem&amp;gt;(employee);            
    }        

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; GenderResolver : ValueResolver&amp;lt;&lt;span class="kwrd"&gt;bool&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;
    {
        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ResolveCore(&lt;span class="kwrd"&gt;bool&lt;/span&gt; source)
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; source ? &lt;span class="str"&gt;"Man"&lt;/span&gt; : &lt;span class="str"&gt;"Female"&lt;/span&gt;;
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; DateFormatter : IValueFormatter
    {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; FormatValue(ResolutionContext context)
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; ((DateTime)context.SourceValue).ToShortDateString();
        }
    }
}&lt;/pre&gt;
Voila! Employee object successfully mapped to EmployeeViewItem:
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-2ScdiUNQG9g/UNhr2sw1SnI/AAAAAAAADKo/cz8yaVp6OeM/s1600/AutoMapper-result.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="AutoMapper example results" border="0" height="116" src="http://2.bp.blogspot.com/-2ScdiUNQG9g/UNhr2sw1SnI/AAAAAAAADKo/cz8yaVp6OeM/s400/AutoMapper-result.png" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://www.mediafire.com/?84on1v3zqq2qdgq" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2012 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/x7GfNO3uuWU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/4008394047225864836/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/12/csharp-object-to-object-mapping-automapper.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/4008394047225864836?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/4008394047225864836?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/x7GfNO3uuWU/csharp-object-to-object-mapping-automapper.html" title="C# Object-to-Object Mapping - AutoMapper Example" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-2ScdiUNQG9g/UNhr2sw1SnI/AAAAAAAADKo/cz8yaVp6OeM/s72-c/AutoMapper-result.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/12/csharp-object-to-object-mapping-automapper.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUcGRXk_eyp7ImA9WhNXFE8.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-4097954890438150954</id><published>2012-12-01T16:31:00.001+02:00</published><updated>2012-12-02T06:37:04.743+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-12-02T06:37:04.743+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Recursion" /><category scheme="http://www.blogger.com/atom/ns#" term="XML" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><title>C# Reading XML Recursively (XmlDocument)</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
I'll be honest - I hate Recursion! But, nothing you can do, there are some situations when you know - this problem should be solved with&amp;nbsp;Recursion. One of such problems is XML reading.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Today I'll show a simple example of reading XmlDocument recursively.&lt;/div&gt;
&lt;br /&gt;
This is our XML:&lt;br /&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;employees&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;employee&lt;/span&gt; &lt;span class="attr"&gt;Id&lt;/span&gt;&lt;span class="kwrd"&gt;="1001"&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="John"&lt;/span&gt; &lt;span class="attr"&gt;Position&lt;/span&gt;&lt;span class="kwrd"&gt;="Developer"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;    
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;projects&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt; &lt;span class="attr"&gt;Code&lt;/span&gt;&lt;span class="kwrd"&gt;="Rocket"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;                
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;7000&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;projects&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;employee&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;employee&lt;/span&gt; &lt;span class="attr"&gt;Id&lt;/span&gt;&lt;span class="kwrd"&gt;="1002"&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="Tomas"&lt;/span&gt; &lt;span class="attr"&gt;Position&lt;/span&gt;&lt;span class="kwrd"&gt;="Helpdesk"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;    
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;projects&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt; &lt;span class="attr"&gt;Code&lt;/span&gt;&lt;span class="kwrd"&gt;="Orlando"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;                
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;1000&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt; &lt;span class="attr"&gt;Code&lt;/span&gt;&lt;span class="kwrd"&gt;="Newman"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;                
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;900&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;projects&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;employee&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;employee&lt;/span&gt; &lt;span class="attr"&gt;Id&lt;/span&gt;&lt;span class="kwrd"&gt;="1003"&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="Susan"&lt;/span&gt; &lt;span class="attr"&gt;Position&lt;/span&gt;&lt;span class="kwrd"&gt;="QA"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;        
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;projects&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;            
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt; &lt;span class="attr"&gt;Code&lt;/span&gt;&lt;span class="kwrd"&gt;="Newman"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;                
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;900&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt; &lt;span class="attr"&gt;Code&lt;/span&gt;&lt;span class="kwrd"&gt;="Rocket"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;                
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;7000&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt; &lt;span class="attr"&gt;Code&lt;/span&gt;&lt;span class="kwrd"&gt;="Orlando"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;                
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;1000&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;budget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;project&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;projects&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;employee&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;employees&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a name='more'&gt;&lt;/a&gt;
&lt;div style="text-align: justify;"&gt;
And, for example, I want to print the names of employees that participated in Orlando project. Of course, this task could be done with &lt;a href="http://www.codearsenal.net/search/label/LINQ"&gt;LINQ&lt;/a&gt;, but this time we want to see some recursion, right? :) Here is the code:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
{
    XmlDocument doc = &lt;span class="kwrd"&gt;new&lt;/span&gt; XmlDocument();
    doc.Load(&lt;span class="str"&gt;"../../Employees.xml"&lt;/span&gt;);
    XmlNode root = doc.SelectSingleNode(&lt;span class="str"&gt;"*"&lt;/span&gt;); 
    ReadXML(root);
}

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ReadXML(XmlNode root)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (root &lt;span class="kwrd"&gt;is&lt;/span&gt; XmlElement)
    {
        DoWork(root);

        &lt;span class="kwrd"&gt;if&lt;/span&gt; (root.HasChildNodes)
            ReadXML(root.FirstChild);
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (root.NextSibling != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            ReadXML(root.NextSibling);
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (root &lt;span class="kwrd"&gt;is&lt;/span&gt; XmlText)
    {}
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (root &lt;span class="kwrd"&gt;is&lt;/span&gt; XmlComment)
    {}
}

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoWork(XmlNode node)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (node.Attributes[&lt;span class="str"&gt;"Code"&lt;/span&gt;] != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        &lt;span class="kwrd"&gt;if&lt;/span&gt;(node.Name == &lt;span class="str"&gt;"project"&lt;/span&gt; &amp;amp;&amp;amp; node.Attributes[&lt;span class="str"&gt;"Code"&lt;/span&gt;].Value == &lt;span class="str"&gt;"Orlando"&lt;/span&gt;)
            Console.WriteLine(node.ParentNode.ParentNode.Attributes[&lt;span class="str"&gt;"Name"&lt;/span&gt;].Value);
}&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-dWa1n_TEpnk/ULoTOqpORAI/AAAAAAAADCM/kAbX8837MuM/s1600/Output.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Reading XML recursively in C# - result" border="0" height="203" src="http://4.bp.blogspot.com/-dWa1n_TEpnk/ULoTOqpORAI/AAAAAAAADCM/kAbX8837MuM/s320/Output.jpg" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
So, I go over each XmlNode by calling &lt;span style="font-family: Courier New, Courier, monospace;"&gt;XMLRead&lt;/span&gt; method. Of course, we need some logic, which is in &lt;span style="font-family: Courier New, Courier, monospace;"&gt;DoWork&lt;/span&gt; method. Inside the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;XMLRead &lt;/span&gt;method I specified that I'm interesting only in XmlElement. As you can see from the source code, you can specify not only XmlElement, but also XmlComment or other XmlNode type, if you need.
All this stuff looks very simple until you starting to remove XML elements. Don't forget that once you removed an XML node, the whole XML structure is changed. 
&lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://www.mediafire.com/?48yrkcjxp10c6ro" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2012 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/xjgvaHtgbJs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/4097954890438150954/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/12/csharp-reading-xml-document-recursively.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/4097954890438150954?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/4097954890438150954?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/xjgvaHtgbJs/csharp-reading-xml-document-recursively.html" title="C# Reading XML Recursively (XmlDocument)" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-dWa1n_TEpnk/ULoTOqpORAI/AAAAAAAADCM/kAbX8837MuM/s72-c/Output.jpg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/12/csharp-reading-xml-document-recursively.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUUMSHs9eip7ImA9WhNQF0s.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-6839697919710445687</id><published>2012-11-24T15:20:00.000+02:00</published><updated>2012-11-24T15:21:29.562+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-11-24T15:21:29.562+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Task" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><category scheme="http://www.blogger.com/atom/ns#" term="Multithreading" /><title>WPF Start New Background Process using Task</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
In GUI programming there is exist some very common problem - if some operation takes too much time the GUI will become unresponsive. For example, if you want establish connection with a server, or fetch some data from database or a large file, this probably will take some time and if you perform this operation in the main thread your GUI will stuck. In order to keep your GUI responsive you need to run long-running operations on a separate thread. So far, in order to open a new thread we used &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Thread&lt;/span&gt; class or &lt;span style="font-family: Courier New, Courier, monospace;"&gt;BackgroundWorker&lt;/span&gt;&lt;span style="font-family: inherit;"&gt;.&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: inherit;"&gt;Starting from .NET Framework 4.0 we have a &lt;/span&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;Task&lt;/span&gt;&lt;span style="font-family: inherit;"&gt; class for this purpose.&lt;/span&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style="font-family: inherit;"&gt;I&lt;/span&gt;f you already switched to Visual Studio 2012 with Framework 4.5 then read my previous post - &lt;a href="http://www.codearsenal.net/2012/11/csharp-5-async-and-await-example.html"&gt;C# Async Programming Example in WPF (Async, Await)&lt;/a&gt;.&lt;br /&gt;
If you still working with Visual Studio 2010 Framework 4.0 then this post is for you.&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;Today I'll show how to build WPF application with background thread using &lt;/span&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;Task&lt;/span&gt;&lt;span style="font-family: inherit;"&gt; Framework 4.0.&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-ii17SqyhrKI/UK-tVTAz5sI/AAAAAAAAC9w/GcWMoQEFU2s/s1600/Wpf-task.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="WPF with new background process using Task Framework 4.0" border="0" src="http://3.bp.blogspot.com/-ii17SqyhrKI/UK-tVTAz5sI/AAAAAAAAC9w/GcWMoQEFU2s/s1600/Wpf-task.jpg" title="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;a name='more'&gt;&lt;/a&gt;
&lt;div style="text-align: justify;"&gt;
Ok, let's say we have a WPF application with a button which&amp;nbsp;triggers&amp;nbsp;some long-running method.&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Here is the XAML:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Window&lt;/span&gt; &lt;span class="attr"&gt;x:Class&lt;/span&gt;&lt;span class="kwrd"&gt;="WpfTask.MainWindow"&lt;/span&gt;
        &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/span&gt;
        &lt;span class="attr"&gt;xmlns:x&lt;/span&gt;&lt;span class="kwrd"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;/span&gt;
        &lt;span class="attr"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;="CodeArsenal.net"&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="200"&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;="300"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DockPanel&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="4"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Button&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="buttonNewTask"&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="50"&lt;/span&gt; 
                &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;="New Task"&lt;/span&gt; &lt;span class="attr"&gt;DockPanel&lt;/span&gt;.&lt;span class="attr"&gt;Dock&lt;/span&gt;&lt;span class="kwrd"&gt;="Top"&lt;/span&gt; 
                &lt;span class="attr"&gt;Click&lt;/span&gt;&lt;span class="kwrd"&gt;="buttonNewTask_Click"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBox&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="textBoxResults"&lt;/span&gt; &lt;span class="attr"&gt;DockPanel&lt;/span&gt;.&lt;span class="attr"&gt;Dock&lt;/span&gt;&lt;span class="kwrd"&gt;="Bottom"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="0,10,0,0"&lt;/span&gt;
                 &lt;span class="attr"&gt;TextWrapping&lt;/span&gt;&lt;span class="kwrd"&gt;="Wrap"&lt;/span&gt;
                 &lt;span class="attr"&gt;FontFamily&lt;/span&gt;&lt;span class="kwrd"&gt;="Lucida Console"&lt;/span&gt; 
                 &lt;span class="attr"&gt;FontSize&lt;/span&gt;&lt;span class="kwrd"&gt;="10"&lt;/span&gt;
                 &lt;span class="attr"&gt;AcceptsReturn&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DockPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Window&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
So, by pressing on &lt;span style="font-family: Courier New, Courier, monospace;"&gt;buttonNewTask&lt;/span&gt; we call for &lt;span style="font-family: Courier New, Courier, monospace;"&gt;buttonNewTask_Click&lt;/span&gt; method. Now we want run this method in separate thread and after the method is completed we want to show the result. This is how to do it in .NET Framework 4.0 using &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Task&lt;/span&gt;:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; buttonNewTask_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)
{
    textBoxResults.Text = &lt;span class="str"&gt;"Working..."&lt;/span&gt;;
    buttonNewTask.IsEnabled = &lt;span class="kwrd"&gt;false&lt;/span&gt;;

    TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext();
    CancellationToken cancelationToken = &lt;span class="kwrd"&gt;new&lt;/span&gt; CancellationToken();

    &lt;span class="kwrd"&gt;string&lt;/span&gt; result = &lt;span class="str"&gt;""&lt;/span&gt;;
    Task.Factory.StartNew(() =&amp;gt; GetResultFromServer(&lt;span class="kwrd"&gt;out&lt;/span&gt; result)).
        ContinueWith(w =&amp;gt;
        {
            textBoxResults.Text = result;
            buttonNewTask.IsEnabled = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
        },
        cancelationToken,
        TaskContinuationOptions.None, scheduler);
}

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; GetResultFromServer(&lt;span class="kwrd"&gt;out&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; result)
{
   Thread.Sleep(2500);
   result = &lt;span class="str"&gt;"Data received."&lt;/span&gt;;
}&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;Task.Factory.StartNew&lt;/span&gt; actually starts a mew thread. I specified &lt;span style="font-family: Courier New, Courier, monospace;"&gt;GetResultFromServer&lt;/span&gt; as the one which will run in new thread. Inside ContnueWith block we put everything that should run after the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;GetResultFromServer&lt;/span&gt; is complete - printing the result and enabling the button. We don't use &lt;span style="font-family: Courier New, Courier, monospace;"&gt;CancelationToken&lt;/span&gt; in this particular example, but its mandatory to specify it. We also must declare the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;TaskScheduler&lt;/span&gt; which tells to Task Framework what our GUI thread is, so it know when to run the the final update.
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-hPYLr8k818Q/ULC66ngOCGI/AAAAAAAAC-I/E3ISl9tPbWY/s1600/Wpf-task-final.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="WPF with new background process using Task Framework 4.0 - Thread completed" border="0" src="http://2.bp.blogspot.com/-hPYLr8k818Q/ULC66ngOCGI/AAAAAAAAC-I/E3ISl9tPbWY/s1600/Wpf-task-final.jpg" title="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://www.mediafire.com/download.php?xz7p7cpvncxepc5" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2010 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/yo3P35d1C_E" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/6839697919710445687/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/11/wpf-start-new-background-process-using-task.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/6839697919710445687?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/6839697919710445687?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/yo3P35d1C_E/wpf-start-new-background-process-using-task.html" title="WPF Start New Background Process using Task" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-ii17SqyhrKI/UK-tVTAz5sI/AAAAAAAAC9w/GcWMoQEFU2s/s72-c/Wpf-task.jpg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/11/wpf-start-new-background-process-using-task.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0UGRng7eSp7ImA9WhNQF0o.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-7342852543462059550</id><published>2012-11-18T16:40:00.000+02:00</published><updated>2012-11-24T18:40:27.601+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-11-24T18:40:27.601+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><category scheme="http://www.blogger.com/atom/ns#" term="Await" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Task" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><category scheme="http://www.blogger.com/atom/ns#" term="Async" /><category scheme="http://www.blogger.com/atom/ns#" term="Multithreading" /><title>C# Async Programming Example in WPF (Async, Await)</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
C# 5.0 and .NET Framework 4.5 bring to developer a very nice feature called Asynchronous Programming. Asynchronous Programming is based on Task Parallel Library (TPL). As you probably know traditional techniques for writing asynchronous programs are very complicated in many aspects, so Async Programming comes to make our life easier when we write Multithreading applications.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
Today we'll see a very simple example of Async Programming using Async, Await and Task keywords in WPF application.
Let's build a simple WPF application with a button and a textbox. By pressing the button we call for some slow method (I named it SlowDude :). I don't want GUI will be locked until the method will finish his job. In other words I want to call that method asynchronously.
&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-NvyKS5wiRz8/UKgMSWMZQxI/AAAAAAAAC3k/iIpAzPSzsUA/s1600/Application.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="C# 5.0 Asynchronous Programming with WPF GUI" border="0" height="168" src="http://4.bp.blogspot.com/-NvyKS5wiRz8/UKgMSWMZQxI/AAAAAAAAC3k/iIpAzPSzsUA/s320/Application.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;a name='more'&gt;&lt;/a&gt;
&lt;br /&gt;
Ok here is our simple XAML:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Window&lt;/span&gt; &lt;span class="attr"&gt;x:Class&lt;/span&gt;&lt;span class="kwrd"&gt;="WpfAsync.MainWindow"&lt;/span&gt;
        &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/span&gt;
        &lt;span class="attr"&gt;xmlns:x&lt;/span&gt;&lt;span class="kwrd"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;/span&gt;
        &lt;span class="attr"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;="CodeArsenal.net"&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="180"&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;="360"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DockPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Button&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="buttonAsync"&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="50"&lt;/span&gt; &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;="Async Example"&lt;/span&gt; 
                &lt;span class="attr"&gt;DockPanel&lt;/span&gt;.&lt;span class="attr"&gt;Dock&lt;/span&gt;&lt;span class="kwrd"&gt;="Top"&lt;/span&gt; &lt;span class="attr"&gt;Click&lt;/span&gt;&lt;span class="kwrd"&gt;="buttonAsync_Click"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBox&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="textBoxResults"&lt;/span&gt; &lt;span class="attr"&gt;DockPanel&lt;/span&gt;.&lt;span class="attr"&gt;Dock&lt;/span&gt;&lt;span class="kwrd"&gt;="Bottom"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="0,20,0,0"&lt;/span&gt;
                 &lt;span class="attr"&gt;TextWrapping&lt;/span&gt;&lt;span class="kwrd"&gt;="Wrap"&lt;/span&gt;
                 &lt;span class="attr"&gt;FontFamily&lt;/span&gt;&lt;span class="kwrd"&gt;="Lucida Console"&lt;/span&gt; 
                 &lt;span class="attr"&gt;FontSize&lt;/span&gt;&lt;span class="kwrd"&gt;="10"&lt;/span&gt;
                 &lt;span class="attr"&gt;AcceptsReturn&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DockPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Window&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
So, as you can see, by clicking on the button we will fire &lt;b&gt;&lt;i&gt;buttonAsync_Click&lt;/i&gt;&lt;/b&gt; method.&lt;br /&gt;
Let's see now the code behind:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; async &lt;span class="kwrd"&gt;void&lt;/span&gt; buttonAsync_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)
{
    buttonAsync.IsEnabled = &lt;span class="kwrd"&gt;false&lt;/span&gt;;
    
    &lt;span class="kwrd"&gt;var&lt;/span&gt; slowTask = Task&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;.Factory.StartNew(() =&amp;gt; SlowDude());
    
    textBoxResults.Text += &lt;span class="str"&gt;"Doing other things while waiting for that slow dude...\r\n"&lt;/span&gt;;
    
    await slowTask;
    
    textBoxResults.Text += slowTask.Result.ToString();
    
    buttonAsync.IsEnabled = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
}

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; SlowDude()
{
    Thread.Sleep(2000);
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Ta-dam! Here I am!\r\n"&lt;/span&gt;;
}&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
Did you notice the &lt;b&gt;&lt;i&gt;async&lt;/i&gt;&lt;/b&gt; modifier in &lt;i&gt;&lt;b&gt;buttonAsync_Click&lt;/b&gt;&lt;/i&gt; method signature? This is crucial, because &lt;i&gt;&lt;b&gt;async&lt;/b&gt;&lt;/i&gt; modifier tells to compiler that this method contains code that will run asynchronously using &lt;b style="font-style: italic;"&gt;await&lt;/b&gt; keyword.&amp;nbsp;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Following code line actually calls for SlowDude method in asynchronous manner:&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; slowTask = Task&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;.Factory.StartNew(() =&amp;gt; SlowDude());    
&lt;/pre&gt;
Ok now let's look on following line:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;await&lt;/span&gt; slowTask;&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
This command means - do other stuff that are between these two lines of code, without locking the main thread, and once &lt;i&gt;&lt;b&gt;SlowDude&lt;/b&gt;&lt;/i&gt; is finished proceed further.
&lt;br /&gt;&lt;br /&gt;
Well, I think, Async Programming is a great thing, and will make our life a lot easier when it comes to Multithreading problems. Well done, Microsoft!
&lt;br /&gt;
&lt;a href="http://www.mediafire.com/?du0jjov32x1qud3" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2012 project).
&lt;br /&gt;&lt;br /&gt;
If you still working with Visual Studio 2010 Framework 4.0 then take a look at this post - &lt;a href="http://www.codearsenal.net/2012/11/wpf-start-new-background-process-using-task.html"&gt;WPF Start New Background Process using Task&lt;/a&gt;
&lt;/div&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/3EiPrgw7MH8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/7342852543462059550/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/11/csharp-5-async-and-await-example.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/7342852543462059550?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/7342852543462059550?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/3EiPrgw7MH8/csharp-5-async-and-await-example.html" title="C# Async Programming Example in WPF (Async, Await)" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-NvyKS5wiRz8/UKgMSWMZQxI/AAAAAAAAC3k/iIpAzPSzsUA/s72-c/Application.JPG" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/11/csharp-5-async-and-await-example.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Dk8HR3o8fCp7ImA9WhNQEEU.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-9058256140128367511</id><published>2012-11-16T18:29:00.000+02:00</published><updated>2012-11-16T18:53:56.474+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-11-16T18:53:56.474+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><category scheme="http://www.blogger.com/atom/ns#" term="Performance" /><title>C# Application Performance Wizard in Visual Studio</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
If you want to know how much resources your &lt;a href="http://www.codearsenal.net/search/label/C%23"&gt;C#&lt;/a&gt; application consumes there is a very nice tool in Visual Studio for this purpose. It allows you to see how much CPU your program uses, amount of memory allocated for it, how much time your program running and many other useful things. Today I'll show how to build a performance test for your &lt;a href="http://www.codearsenal.net/search/label/C%23"&gt;C#&lt;/a&gt; program in Visual Studio.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Ok, let's suppose we've created some &lt;a href="http://www.codearsenal.net/search/label/C%23"&gt;C#&lt;/a&gt; application in Visual Studio. In order to run Performance Wizard go to main menu and click on &lt;i&gt;&lt;b&gt;Analyze&lt;/b&gt;&lt;/i&gt; and &lt;b&gt;&lt;i&gt;Launch Performance Wizard&lt;/i&gt;&lt;/b&gt;.
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-_pO4TkeJmSo/UKZVvGWsLiI/AAAAAAAAC14/iFtmrnu4mRk/s1600/launch-performance-wizard.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Launch Performance Wizard in Visual Studio 2010" border="0" height="148" src="http://2.bp.blogspot.com/-_pO4TkeJmSo/UKZVvGWsLiI/AAAAAAAAC14/iFtmrnu4mRk/s400/launch-performance-wizard.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;
You'll see Performance Wizard first page where you can choose desired profiling method. Usually we want to know CPU consuming or Memory allocation. Choose your profiling method and click &lt;b&gt;&lt;i&gt;Next&lt;/i&gt;&lt;/b&gt;.
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-bJKSfywjKOk/UKZcPipGfgI/AAAAAAAAC2M/0GtFjBW2ePI/s1600/specify-profiling-method.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Visual Studio 2010 Performance Wizard Page 1" border="0" height="345" src="http://1.bp.blogspot.com/-bJKSfywjKOk/UKZcPipGfgI/AAAAAAAAC2M/0GtFjBW2ePI/s400/specify-profiling-method.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
On the second page you can choose specific target project for profiling or even some external executable if you need it. Click &lt;b style="font-style: italic;"&gt;Next&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/--bGrxKqV3Lo/UKZe7OLl2hI/AAAAAAAAC2c/20Eav_DKLyY/s1600/choose-application.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Visual Studio 2010 Performance Wizard Page 2" border="0" height="345" src="http://4.bp.blogspot.com/--bGrxKqV3Lo/UKZe7OLl2hI/AAAAAAAAC2c/20Eav_DKLyY/s400/choose-application.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Middle */
google_ad_slot = "2969384752";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;br /&gt;
On the third page, Performance Wizard asks you if you wish to launch profiling process after pressing &lt;b&gt;&lt;i&gt;Finish &lt;/i&gt;&lt;/b&gt;button. Check the&amp;nbsp;check-box&amp;nbsp;and press &lt;b&gt;&lt;i&gt;Finish&lt;/i&gt;&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-L-HDa95xrEU/UKZej6LJxhI/AAAAAAAAC2U/1CcZZBcVTkE/s1600/specify-settings.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Visual Studio 2010 Performance Wizard Page 3" border="0" height="347" src="http://2.bp.blogspot.com/-L-HDa95xrEU/UKZej6LJxhI/AAAAAAAAC2U/1CcZZBcVTkE/s400/specify-settings.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Here what you'll see if you choose &lt;b&gt;&lt;i&gt;CPU Sampling&lt;/i&gt;&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-Ldr4PDUofmc/UKZg1_MlxfI/AAAAAAAAC2k/wbgzO9UWbvo/s1600/performance-results-cpu.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Visual Studio 2010 Performance Results - CPU Sampling" border="0" height="321" src="http://1.bp.blogspot.com/-Ldr4PDUofmc/UKZg1_MlxfI/AAAAAAAAC2k/wbgzO9UWbvo/s400/performance-results-cpu.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
If you choose &lt;b style="font-style: italic;"&gt;.NET Memory Allocation&lt;/b&gt;&amp;nbsp;this is what you'll see:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-6u8NyIVVn0E/UKZh_zNnGUI/AAAAAAAAC2s/r7hG9_rHKMM/s1600/performance-results-memory.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Visual Studio 2010 Performance Results - .NET Memory Allocation" border="0" height="311" src="http://1.bp.blogspot.com/-6u8NyIVVn0E/UKZh_zNnGUI/AAAAAAAAC2s/r7hG9_rHKMM/s400/performance-results-memory.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
If click on &lt;i style="font-weight: bold;"&gt;Show Hot Lines&lt;/i&gt;&amp;nbsp;you'll see detailed information about each method of your program:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-eklYfm2Vof4/UKZnR7XCymI/AAAAAAAAC3E/8jVAgNagp4g/s1600/hot-lines.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Visual Studio 2010 Performance Wizard Results - Hot Lines" border="0" height="102" src="http://3.bp.blogspot.com/-eklYfm2Vof4/UKZnR7XCymI/AAAAAAAAC3E/8jVAgNagp4g/s400/hot-lines.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Another useful feature is &lt;b style="font-style: italic;"&gt;Trimmed Call Tree&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-H9j_FP-mc70/UKZoYD79QII/AAAAAAAAC3M/yXGvBHr_00c/s1600/trimmed-call-tree.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Visual Studio 2010 Performance Wizard Results - Trimmed Call Tree" border="0" height="107" src="http://2.bp.blogspot.com/-H9j_FP-mc70/UKZoYD79QII/AAAAAAAAC3M/yXGvBHr_00c/s400/trimmed-call-tree.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/wylkb9FBdJk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/9058256140128367511/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/11/csharp-application-performance-wizard-in-visual-studio.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/9058256140128367511?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/9058256140128367511?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/wylkb9FBdJk/csharp-application-performance-wizard-in-visual-studio.html" title="C# Application Performance Wizard in Visual Studio" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-_pO4TkeJmSo/UKZVvGWsLiI/AAAAAAAAC14/iFtmrnu4mRk/s72-c/launch-performance-wizard.JPG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/11/csharp-application-performance-wizard-in-visual-studio.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQNQnkycSp7ImA9WhNRFEU.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-9176169776490909636</id><published>2012-11-09T21:05:00.002+02:00</published><updated>2012-11-09T21:13:13.799+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-11-09T21:13:13.799+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Regex" /><title>7 Regular Expressions a C# Developer Must Know</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
Regular expressions, aka Regex, are very commonly used by any developer no matter what language he's writing his program. Today I'll show the most useful Regex that sooner or later you will need them.&lt;/div&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;1. &lt;b&gt;&lt;i&gt;&lt;u&gt;URL Regex&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;:&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Match&lt;/b&gt;:&lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;http://www.codearsenal.net/&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Not Match&lt;/b&gt;: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;http://www.codearsenal.net?/&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;2. &lt;b&gt;&lt;i&gt;&lt;u&gt;IP Address Regex&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;:&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Match&lt;/b&gt;:&lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;73.180.213.135&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Not Match&lt;/b&gt;: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;255.255.255.256&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;3. &lt;b&gt;&lt;i&gt;&lt;u&gt;Email Address Regex&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;:&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Match&lt;/b&gt;:&lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;john@codearsenal.net&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Not Match&lt;/b&gt;: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;john@gmail?com&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;4. &lt;b&gt;&lt;i&gt;&lt;u&gt;Username Regex&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;:&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;^[a-z0-9_-]{3,16}$&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Match&lt;/b&gt;:&lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;john_detroit-13&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Not Match&lt;/b&gt;: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;this-username-is-too-long&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;5. &lt;b&gt;&lt;i&gt;&lt;u&gt;Password Regex&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;:&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[_!@#$%^&amp;amp;+=]).*$&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Description&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Password length must be at least 8 characters.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Password must contain at least one lower case letter, at least one upper case letter and at least one special character.&lt;/li&gt;
&lt;li&gt;Allowed aspecial characters: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;_!@#$%^&amp;amp;+=&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;Match&lt;/b&gt;:&lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;paSS_123&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Not Match&lt;/b&gt;: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;weak_pass&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Middle */
google_ad_slot = "2969384752";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;6. &lt;b&gt;&lt;i&gt;&lt;u&gt;Between Two Characters Regex&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;:&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;(?&amp;lt;=\[)(.*?)(?=\])&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Description&lt;/b&gt;:&lt;br /&gt;
Gets a string that is between two specific characters without including them, in our case these are two brackets.
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Match&lt;/b&gt;:&lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;[anything between these brackets]&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Not Match&lt;/b&gt;: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;no brackets here&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;7. &lt;b&gt;&lt;i&gt;&lt;u&gt;File Extension Regex&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;:&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;([^\s]+(\.(?i)(jpg|png|gif|bmp))$)&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Description&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Any file name with specific extension, in our case these are image files.&lt;/li&gt;
&lt;li&gt;Allowed extensions: &lt;i&gt;jpg&lt;/i&gt;, &lt;i&gt;png&lt;/i&gt;, &lt;i&gt;gif&lt;/i&gt; or &lt;i&gt;bmp&lt;/i&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;You can change the allowed extensions in the regex on those what you need.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;Match&lt;/b&gt;:&lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;MyPicture.bmp&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Not Match&lt;/b&gt;: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;br /&gt;app.xaml&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
When you want to test your Regex I suggest to use &lt;a href="http://rubular.com/" rel="nofollow" target="_blank"&gt;Rubular&lt;/a&gt; online tool. It has user friendly interface and what I like the most in it is that it's showing the matching result on the fly.&lt;/div&gt;
&lt;br /&gt;
And this is an example of how to use Regex in C#:
&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;IsMatch Method&lt;/b&gt;&lt;/u&gt;:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;string&lt;/span&gt; regexPattern = &lt;span class="str"&gt;@"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"&lt;/span&gt;;

&lt;span class="kwrd"&gt;if&lt;/span&gt; (Regex.IsMatch(testString, regexPattern))
    Console.WriteLine(&lt;span class="str"&gt;"This is an image file name."&lt;/span&gt;);
&lt;span class="kwrd"&gt;else&lt;/span&gt;
    Console.WriteLine(&lt;span class="str"&gt;"This is not an image file name."&lt;/span&gt;);&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;Match Method&lt;/b&gt;&lt;/u&gt;:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;Regex regex = &lt;span class="kwrd"&gt;new&lt;/span&gt; Regex(&lt;span class="str"&gt;@"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"&lt;/span&gt;);
Match match = regex.Match(testString);

&lt;span class="kwrd"&gt;if&lt;/span&gt; (match.Success)
    Console.WriteLine(match.Groups[1].Value);&lt;/pre&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/0TiHpDitbns" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/9176169776490909636/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/11/7-regular-expressions-csharp-developer-must-know.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/9176169776490909636?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/9176169776490909636?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/0TiHpDitbns/7-regular-expressions-csharp-developer-must-know.html" title="7 Regular Expressions a C# Developer Must Know" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/11/7-regular-expressions-csharp-developer-must-know.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkcCR3o_cCp7ImA9WhNSGUo.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-8103834652068592554</id><published>2012-11-03T22:21:00.000+02:00</published><updated>2012-11-03T22:21:06.448+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-11-03T22:21:06.448+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><category scheme="http://www.blogger.com/atom/ns#" term="Enum" /><category scheme="http://www.blogger.com/atom/ns#" term="XAML" /><category scheme="http://www.blogger.com/atom/ns#" term="ObjectDataProvider" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><title>WPF ObjectDataProvider - Binding Enum to ComboBox</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
Let's say we created some &lt;i&gt;Enum &lt;/i&gt;data type. Now we want to show our &lt;i&gt;Enum &lt;/i&gt;values on &lt;a href="http://www.codearsenal.net/search/label/WPF"&gt;WPF&lt;/a&gt; &lt;i&gt;ComboBox &lt;/i&gt;or &lt;i&gt;ListBox&lt;/i&gt;. How do we do that?&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
In order to achieve this, in &lt;a href="http://www.codearsenal.net/search/label/WPF"&gt;WPF&lt;/a&gt; we have an &lt;i&gt;ObjectDataProvider&lt;/i&gt; class which provides mechanism for creation a &lt;a href="http://www.codearsenal.net/search/label/XAML"&gt;XAML&lt;/a&gt; object available as a binding source.&lt;br /&gt;
Here is our &lt;i&gt;Enum:&lt;/i&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;enum&lt;/span&gt; TimePeriods
{
    Year,
    Month,
    Day,
    Hour,
    Minute
}&lt;/pre&gt;
Now let's see how we bind it to a ComboBox in &lt;a href="http://www.codearsenal.net/search/label/XAML"&gt;XAML&lt;/a&gt;:
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Window&lt;/span&gt; &lt;span class="attr"&gt;x:Class&lt;/span&gt;&lt;span class="kwrd"&gt;="WPFObjectDataProvider.MainWindow"&lt;/span&gt;
        &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/span&gt;
        &lt;span class="attr"&gt;xmlns:x&lt;/span&gt;&lt;span class="kwrd"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;/span&gt;
        &lt;span class="attr"&gt;xmlns:sys&lt;/span&gt;&lt;span class="kwrd"&gt;="clr-namespace:System;assembly=mscorlib"&lt;/span&gt;
        &lt;span class="attr"&gt;xmlns:local&lt;/span&gt;&lt;span class="kwrd"&gt;="clr-namespace:WPFObjectDataProvider"&lt;/span&gt;
        &lt;span class="attr"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;="CodeArsenal.Net"&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="59"&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;="266"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Window.Resources&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ObjectDataProvider&lt;/span&gt; &lt;span class="attr"&gt;x:Key&lt;/span&gt;&lt;span class="kwrd"&gt;="timePeriodsFromEnum"&lt;/span&gt;
                            &lt;span class="attr"&gt;MethodName&lt;/span&gt;&lt;span class="kwrd"&gt;="GetValues"&lt;/span&gt; 
                            &lt;span class="attr"&gt;ObjectType&lt;/span&gt;&lt;span class="kwrd"&gt;="{x:Type sys:Enum}"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ObjectDataProvider.MethodParameters&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;x:Type&lt;/span&gt; &lt;span class="attr"&gt;TypeName&lt;/span&gt;&lt;span class="kwrd"&gt;="local:TimePeriods"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ObjectDataProvider.MethodParameters&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ObjectDataProvider&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Window.Resources&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ComboBox&lt;/span&gt; &lt;span class="attr"&gt;ItemsSource&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Source={StaticResource timePeriodsFromEnum}}"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ComboBox&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Window&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
As you can see in the code above I added &lt;i&gt;ObjectDataProvider&lt;/i&gt; to &lt;i&gt;WIndow.Resources&lt;/i&gt;. The &lt;i&gt;GetValues()&lt;/i&gt;&amp;nbsp;method gets an array of the values in an&amp;nbsp;&lt;i&gt;Enum.&lt;/i&gt;&amp;nbsp;Since &lt;i&gt;Enum&lt;/i&gt; is part of &lt;i&gt;mscorlib&lt;/i&gt; assembly we must add a reference to it in our &lt;a href="http://www.codearsenal.net/search/label/XAML"&gt;XAML&lt;/a&gt;. Finally we bind our static &lt;i&gt;Enum&lt;/i&gt; resource to &lt;i&gt;ItemSource&lt;/i&gt; of the &lt;i&gt;ComboBox&lt;/i&gt;. This is what we have when we run the application:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-KL3jb0uT8kI/UJV6zXynrkI/AAAAAAAACtI/_KmZFSqMJA4/s1600/Application.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="WPF ObjectDataProvider - Binding Enum to ComboBox" border="0" src="http://2.bp.blogspot.com/-KL3jb0uT8kI/UJV6zXynrkI/AAAAAAAACtI/_KmZFSqMJA4/s1600/Application.JPG" title="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;a href="http://www.mediafire.com/?m5a35pkl83ijd8y" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2010 Project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/xdM5CMBBRrg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/8103834652068592554/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/11/wpf-objectdataprovider-binding-enum-to-combobox.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8103834652068592554?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8103834652068592554?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/xdM5CMBBRrg/wpf-objectdataprovider-binding-enum-to-combobox.html" title="WPF ObjectDataProvider - Binding Enum to ComboBox" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-KL3jb0uT8kI/UJV6zXynrkI/AAAAAAAACtI/_KmZFSqMJA4/s72-c/Application.JPG" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/11/wpf-objectdataprovider-binding-enum-to-combobox.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0cDSH4zfSp7ImA9WhNSFUk.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-8406015467075184308</id><published>2012-10-20T17:29:00.003+02:00</published><updated>2012-10-29T22:04:39.085+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-29T22:04:39.085+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Configuration Settings" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="App.Config" /><title>C# Writing Custom Configuration Settings Section</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
Almost every &lt;a href="http://www.codearsenal.net/search/label/C%23"&gt;C#&lt;/a&gt; application has its own Configuration Settings file also known as &lt;a href="http://www.codearsenal.net/search/label/App.Config"&gt;App.Config&lt;/a&gt;. If you look on your &lt;a href="http://www.codearsenal.net/search/label/App.Config"&gt;App.Config&lt;/a&gt; file you probably will see a list of key value pairs:&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;add&lt;/span&gt; &lt;span class="attr"&gt;key&lt;/span&gt;&lt;span class="kwrd"&gt;="key0"&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="value0"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;add&lt;/span&gt; &lt;span class="attr"&gt;key&lt;/span&gt;&lt;span class="kwrd"&gt;="key1"&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="value1"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; 
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;add&lt;/span&gt; &lt;span class="attr"&gt;key&lt;/span&gt;&lt;span class="kwrd"&gt;="key2"&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="value2"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
... &lt;/pre&gt;
But what if you need some more complex configurations? Something like this:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySection&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySettings&lt;/span&gt; &lt;span class="attr"&gt;MyKey&lt;/span&gt;&lt;span class="kwrd"&gt;="John"&lt;/span&gt; &lt;span class="attr"&gt;MyValue1&lt;/span&gt;&lt;span class="kwrd"&gt;="Developer"&lt;/span&gt; &lt;span class="attr"&gt;MyValue2&lt;/span&gt;&lt;span class="kwrd"&gt;="Expert"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySettings&lt;/span&gt; &lt;span class="attr"&gt;MyKey&lt;/span&gt;&lt;span class="kwrd"&gt;="Tomas"&lt;/span&gt; &lt;span class="attr"&gt;MyValue1&lt;/span&gt;&lt;span class="kwrd"&gt;="Developer"&lt;/span&gt; &lt;span class="attr"&gt;MyValue2&lt;/span&gt;&lt;span class="kwrd"&gt;="Junior"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;    
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySettings&lt;/span&gt; &lt;span class="attr"&gt;MyKey&lt;/span&gt;&lt;span class="kwrd"&gt;="Robert"&lt;/span&gt; &lt;span class="attr"&gt;MyValue1&lt;/span&gt;&lt;span class="kwrd"&gt;="Developer"&lt;/span&gt; &lt;span class="attr"&gt;MyValue2&lt;/span&gt;&lt;span class="kwrd"&gt;="Senior"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySettings&lt;/span&gt; &lt;span class="attr"&gt;MyKey&lt;/span&gt;&lt;span class="kwrd"&gt;="Kurt"&lt;/span&gt; &lt;span class="attr"&gt;MyValue1&lt;/span&gt;&lt;span class="kwrd"&gt;="IT"&lt;/span&gt; &lt;span class="attr"&gt;MyValue2&lt;/span&gt;&lt;span class="kwrd"&gt;="Expert"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySettings&lt;/span&gt; &lt;span class="attr"&gt;MyKey&lt;/span&gt;&lt;span class="kwrd"&gt;="Anna"&lt;/span&gt; &lt;span class="attr"&gt;MyValue1&lt;/span&gt;&lt;span class="kwrd"&gt;="HR"&lt;/span&gt; &lt;span class="attr"&gt;MyValue2&lt;/span&gt;&lt;span class="kwrd"&gt;="Senior"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;    
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;MySection&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
Today I'll show how to build this kind of Custom Configuration Settings in &lt;a href="http://www.codearsenal.net/search/label/C%23"&gt;C#&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;div style="text-align: justify;"&gt;
First of all, we have to define our class that will represent the whole configuration section. This class must be inherited from &lt;i&gt;ConfigurationSection&lt;/i&gt; class. By the way, don't forget to include &lt;span style="background-color: white; font-family: Consolas; font-size: 13px;"&gt;System.Configuration&lt;/span&gt; namespace. Also we need an &lt;i&gt;Element&lt;/i&gt; class. This class will represent a single row in the section and it must be inherited from &lt;i&gt;ConfigurationElement&lt;/i&gt; class. Third and the last class will represent the collection of our elements. Let's call it &lt;i&gt;ElementCollection.&lt;/i&gt; This class must be inherited from &lt;i&gt;ConfigurationElementCollection.&lt;/i&gt; Let's see the source code:&lt;/div&gt;
&lt;br /&gt;
&lt;u&gt;Element.cs&lt;/u&gt;:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Element : ConfigurationElement
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ATTRIBUTE_MYKEY = &lt;span class="str"&gt;"MyKey"&lt;/span&gt;;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ATTRIBUTE_MYVALUE1 = &lt;span class="str"&gt;"MyValue1"&lt;/span&gt;;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ATTRIBUTE_MYVALUE2 = &lt;span class="str"&gt;"MyValue2"&lt;/span&gt;;
    
    [ConfigurationProperty(ATTRIBUTE_MYKEY, IsRequired = &lt;span class="kwrd"&gt;true&lt;/span&gt;, IsKey = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]        
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; MyKey
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;)&lt;span class="kwrd"&gt;this&lt;/span&gt;[ATTRIBUTE_MYKEY]; }
        &lt;span class="kwrd"&gt;set&lt;/span&gt; { &lt;span class="kwrd"&gt;this&lt;/span&gt;[ATTRIBUTE_MYKEY] = value; }
    }

    [ConfigurationProperty(ATTRIBUTE_MYVALUE1, IsRequired = &lt;span class="kwrd"&gt;true&lt;/span&gt;, IsKey = &lt;span class="kwrd"&gt;false&lt;/span&gt;)]        
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; MyValue1
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;)&lt;span class="kwrd"&gt;this&lt;/span&gt;[ATTRIBUTE_MYVALUE1]; }
        &lt;span class="kwrd"&gt;set&lt;/span&gt; { &lt;span class="kwrd"&gt;this&lt;/span&gt;[ATTRIBUTE_MYVALUE1] = value; }
    }

    [ConfigurationProperty(ATTRIBUTE_MYVALUE2, IsRequired = &lt;span class="kwrd"&gt;true&lt;/span&gt;, IsKey = &lt;span class="kwrd"&gt;false&lt;/span&gt;)]        
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; MyValue2
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;)&lt;span class="kwrd"&gt;this&lt;/span&gt;[ATTRIBUTE_MYVALUE2]; }
        &lt;span class="kwrd"&gt;set&lt;/span&gt; { &lt;span class="kwrd"&gt;this&lt;/span&gt;[ATTRIBUTE_MYVALUE2] = value; }
    }    
}&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
Notice that above each property there is &lt;i&gt;ConfigurationProperty&lt;/i&gt; attribute. Also &lt;i&gt;MyKey&lt;/i&gt; property has &lt;i&gt;IsKey&lt;/i&gt; attribute set to true. Let's see now &lt;i&gt;ElementCollection&lt;/i&gt; class which represents the collection of elements:&lt;/div&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Middle */
google_ad_slot = "2969384752";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;br /&gt;
&lt;u&gt;ElementCollection.cs&lt;/u&gt;:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ElementCollection : ConfigurationElementCollection
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; ElementCollection()
    {
        &lt;span class="kwrd"&gt;this&lt;/span&gt;.AddElementName = &lt;span class="str"&gt;"MySettings"&lt;/span&gt;;
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; GetElementKey(ConfigurationElement element)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; (element &lt;span class="kwrd"&gt;as&lt;/span&gt; Element).MyKey;
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; ConfigurationElement CreateNewElement()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Element();
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Element &lt;span class="kwrd"&gt;this&lt;/span&gt;[&lt;span class="kwrd"&gt;string&lt;/span&gt; key]
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;base&lt;/span&gt;.BaseGet(key) &lt;span class="kwrd"&gt;as&lt;/span&gt; Element; }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Element &lt;span class="kwrd"&gt;this&lt;/span&gt;[&lt;span class="kwrd"&gt;int&lt;/span&gt; ind]
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;base&lt;/span&gt;.BaseGet(ind) &lt;span class="kwrd"&gt;as&lt;/span&gt; Element; }
    }
}&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
I override &lt;i&gt;GetElementKey&lt;/i&gt; method in order to determine the key attribute in the element. In our case &lt;i&gt;MyKey&lt;/i&gt; attribute is our key in configuration element. And finally here is the code of &lt;i&gt;Section&lt;/i&gt; class:&lt;/div&gt;
&lt;br /&gt;
&lt;u&gt;Section.cs&lt;/u&gt;:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Section : ConfigurationSection
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; sectionName = &lt;span class="str"&gt;"MySection"&lt;/span&gt;;

    [ConfigurationProperty(&lt;span class="str"&gt;""&lt;/span&gt;, IsDefaultCollection = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; ElementCollection MySettings
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt;
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;[&lt;span class="str"&gt;""&lt;/span&gt;] &lt;span class="kwrd"&gt;as&lt;/span&gt; ElementCollection;
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Section GetSection()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; (Section)ConfigurationManager.GetSection(sectionName);
    }
}&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
We almost done. Before we can use our configuration section we must specify the name of our custom configuration section, configuration section class namespace and the assembly name where the configuration section classes are located:
&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configSections&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="MySection"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="CustomConfigurationSection.CustomConfig.Section, CustomConfigurationSection"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configSections&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySection&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySettings&lt;/span&gt; &lt;span class="attr"&gt;MyKey&lt;/span&gt;&lt;span class="kwrd"&gt;="John"&lt;/span&gt; &lt;span class="attr"&gt;MyValue1&lt;/span&gt;&lt;span class="kwrd"&gt;="Developer"&lt;/span&gt; &lt;span class="attr"&gt;MyValue2&lt;/span&gt;&lt;span class="kwrd"&gt;="Expert"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySettings&lt;/span&gt; &lt;span class="attr"&gt;MyKey&lt;/span&gt;&lt;span class="kwrd"&gt;="Tomas"&lt;/span&gt; &lt;span class="attr"&gt;MyValue1&lt;/span&gt;&lt;span class="kwrd"&gt;="Developer"&lt;/span&gt; &lt;span class="attr"&gt;MyValue2&lt;/span&gt;&lt;span class="kwrd"&gt;="Junior"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySettings&lt;/span&gt; &lt;span class="attr"&gt;MyKey&lt;/span&gt;&lt;span class="kwrd"&gt;="Robert"&lt;/span&gt; &lt;span class="attr"&gt;MyValue1&lt;/span&gt;&lt;span class="kwrd"&gt;="Developer"&lt;/span&gt; &lt;span class="attr"&gt;MyValue2&lt;/span&gt;&lt;span class="kwrd"&gt;="Senior"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySettings&lt;/span&gt; &lt;span class="attr"&gt;MyKey&lt;/span&gt;&lt;span class="kwrd"&gt;="Kurt"&lt;/span&gt; &lt;span class="attr"&gt;MyValue1&lt;/span&gt;&lt;span class="kwrd"&gt;="IT"&lt;/span&gt; &lt;span class="attr"&gt;MyValue2&lt;/span&gt;&lt;span class="kwrd"&gt;="Expert"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;MySettings&lt;/span&gt; &lt;span class="attr"&gt;MyKey&lt;/span&gt;&lt;span class="kwrd"&gt;="Anna"&lt;/span&gt; &lt;span class="attr"&gt;MyValue1&lt;/span&gt;&lt;span class="kwrd"&gt;="HR"&lt;/span&gt; &lt;span class="attr"&gt;MyValue2&lt;/span&gt;&lt;span class="kwrd"&gt;="Senior"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;MySection&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
Here is how we access our configurations:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;Console.Write(section.MySettings[&lt;span class="str"&gt;"John"&lt;/span&gt;].MyValue1);
Console.Write(section.MySettings[&lt;span class="str"&gt;"Anna"&lt;/span&gt;].MyValue2);&lt;/pre&gt;
&lt;a href="http://www.mediafire.com/?57eo3dxadw0rac3" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2010 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/yVrGol0nWVY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/8406015467075184308/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/10/writing-custom-configuration-section-in-csharp.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8406015467075184308?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8406015467075184308?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/yVrGol0nWVY/writing-custom-configuration-section-in-csharp.html" title="C# Writing Custom Configuration Settings Section" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/10/writing-custom-configuration-section-in-csharp.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0IFSXczeSp7ImA9WhNTEUk.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-7693559039996261212</id><published>2012-10-13T17:18:00.002+02:00</published><updated>2012-10-13T17:18:38.981+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-13T17:18:38.981+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><category scheme="http://www.blogger.com/atom/ns#" term="XAML" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><title>Speech Synthesizer in WPF</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;

&lt;div style="text-align: justify;"&gt;
I recently came across with some very interesting feature in &lt;a href="http://www.codearsenal.net/search/label/C%23"&gt;C#&lt;/a&gt; - Speech Synthesizer! Speech Synthesizer is Microsoft API which allows to build speech enabled applications. Just one row of code and your &lt;a href="http://www.codearsenal.net/search/label/WPF"&gt;WPF&lt;/a&gt; application will spell any textual data!&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
All you need is just add a reference to &lt;i&gt;System.Speech.Synthesis&lt;/i&gt;&amp;nbsp;and a couple rows of code. I built a simple application just to show how to use Speech Synthesizer in &lt;a href="http://www.codearsenal.net/search/label/WPF"&gt;WPF&lt;/a&gt;.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-PcWMTVuhdbg/UHiPOnnWLdI/AAAAAAAACj4/cUFw5KdFNpA/s1600/SpeechSynthesizer.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Speech Synthesizer in WPF" border="0" height="161" src="http://4.bp.blogspot.com/-PcWMTVuhdbg/UHiPOnnWLdI/AAAAAAAACj4/cUFw5KdFNpA/s320/SpeechSynthesizer.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
Here is the &lt;a href="http://www.codearsenal.net/search/label/XAML"&gt;XAML&lt;/a&gt; code of the GUI:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBox&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Column&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt;
         &lt;span class="attr"&gt;HorizontalAlignment&lt;/span&gt;&lt;span class="kwrd"&gt;="Left"&lt;/span&gt;
         &lt;span class="attr"&gt;TextWrapping&lt;/span&gt;&lt;span class="kwrd"&gt;="Wrap"&lt;/span&gt;
         &lt;span class="attr"&gt;AcceptsReturn&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt;
         &lt;span class="attr"&gt;VerticalScrollBarVisibility&lt;/span&gt;&lt;span class="kwrd"&gt;="Visible"&lt;/span&gt;
         &lt;span class="attr"&gt;FontSize&lt;/span&gt;&lt;span class="kwrd"&gt;="14"&lt;/span&gt;
         &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;="100"&lt;/span&gt;
         &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;="270"&lt;/span&gt;
         &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="TextBox_Read"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Talk to me!&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;TextBox&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Button&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Column&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt;                
        &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="Button_Say"&lt;/span&gt;
        &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;="Say"&lt;/span&gt; &lt;span class="attr"&gt;Click&lt;/span&gt;&lt;span class="kwrd"&gt;="Button_Say_Click"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Button&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Button&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Column&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="2"&lt;/span&gt;                
        &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="Button_Who"&lt;/span&gt;
        &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;="Who's talking?"&lt;/span&gt; &lt;span class="attr"&gt;Click&lt;/span&gt;&lt;span class="kwrd"&gt;="Button_Who_Click"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Button&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Slider&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Column&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; 
        &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="Slider_Volume"&lt;/span&gt;
        &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="50"&lt;/span&gt; &lt;span class="attr"&gt;Minimum&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;Maximum&lt;/span&gt;&lt;span class="kwrd"&gt;="100"&lt;/span&gt;
        &lt;span class="attr"&gt;TickFrequency&lt;/span&gt;&lt;span class="kwrd"&gt;="5"&lt;/span&gt; &lt;span class="attr"&gt;ValueChanged&lt;/span&gt;&lt;span class="kwrd"&gt;="VolumeChanged"&lt;/span&gt;
        &lt;span class="attr"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;="Volume"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Slider&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Column&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;Grid&lt;/span&gt;.&lt;span class="attr"&gt;Row&lt;/span&gt;&lt;span class="kwrd"&gt;="2"&lt;/span&gt; 
        &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="Slider_Rate"&lt;/span&gt;
        &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;Minimum&lt;/span&gt;&lt;span class="kwrd"&gt;="-5"&lt;/span&gt; &lt;span class="attr"&gt;Maximum&lt;/span&gt;&lt;span class="kwrd"&gt;="7"&lt;/span&gt; 
        &lt;span class="attr"&gt;TickFrequency&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; &lt;span class="attr"&gt;ValueChanged&lt;/span&gt;&lt;span class="kwrd"&gt;="RateChanged"&lt;/span&gt;
        &lt;span class="attr"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;="Speech Rate"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;So, when user clicks on "Say" button, the Speech Synthesizer will spell what is currently written in the textbox. The upper slider is volume level and by the second slider we can change the speak rate. Let's see now the code behind:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MainWindow : Window
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; SpeechSynthesizer _speechSynthesizer = &lt;span class="kwrd"&gt;new&lt;/span&gt; SpeechSynthesizer();

    &lt;span class="kwrd"&gt;public&lt;/span&gt; MainWindow()
    {
        InitializeComponent();            
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Button_Say_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)
    {
        _speechSynthesizer.SpeakAsync(TextBox_Read.Text);
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; VolumeChanged(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedPropertyChangedEventArgs&amp;lt;&lt;span class="kwrd"&gt;double&lt;/span&gt;&amp;gt; e)
    {
        _speechSynthesizer.Volume = (&lt;span class="kwrd"&gt;int&lt;/span&gt;)((Slider)e.OriginalSource).Value;
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RateChanged(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedPropertyChangedEventArgs&amp;lt;&lt;span class="kwrd"&gt;double&lt;/span&gt;&amp;gt; e)
    {
        _speechSynthesizer.Rate = (&lt;span class="kwrd"&gt;int&lt;/span&gt;)((Slider)e.OriginalSource).Value;
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Button_Who_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)
    {
        _speechSynthesizer.SpeakAsync(&lt;span class="str"&gt;"This is "&lt;/span&gt; + _speechSynthesizer.Voice.Name);
    }
}&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
As you can see it is very easy to use speech synthesizer in WPF application. Just pass the text to &lt;i&gt;SpeakAsync&lt;/i&gt; method and this is it!&lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://www.mediafire.com/?bvyavbk4f8e7m3x" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2010 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/P3DjY4q1Y1g" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/7693559039996261212/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/10/speech-synthesizer-in-wpf.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/7693559039996261212?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/7693559039996261212?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/P3DjY4q1Y1g/speech-synthesizer-in-wpf.html" title="Speech Synthesizer in WPF" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-PcWMTVuhdbg/UHiPOnnWLdI/AAAAAAAACj4/cUFw5KdFNpA/s72-c/SpeechSynthesizer.JPG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/10/speech-synthesizer-in-wpf.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkMMQXo6fyp7ImA9WhNTEE4.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-8795968005413637858</id><published>2012-09-26T16:24:00.003+02:00</published><updated>2012-10-12T10:28:00.417+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-12T10:28:00.417+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGrid" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><category scheme="http://www.blogger.com/atom/ns#" term="WCF" /><title>WCF Service Returns List of Objects to WPF DataGrid</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
Today we will build WCF service that returns a list of objects, an Employee objects, in our case. And we will build a WPF client application which will show the result in DataGrid.&lt;/div&gt;
&lt;br /&gt;
First, let's create WCF service. Choose WCF Service Application from the list:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-ZwrDTh08yqM/UGL84Bt6CxI/AAAAAAAACaE/bnyYVtl9T48/s1600/Add-new-project-WCF-service.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Add new project (WCF Service)" border="0" height="212" src="http://4.bp.blogspot.com/-ZwrDTh08yqM/UGL84Bt6CxI/AAAAAAAACaE/bnyYVtl9T48/s320/Add-new-project-WCF-service.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Visual Studio will automatically create template project with IService.cs and Service.svc files, where actually all the service logic will be written. I added the Employee class and edited Service.cs and IService.cs in order to service be able to return a list of employees. Let's see:&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;i style="text-align: left;"&gt;&lt;u&gt;Employee.cs:&lt;/u&gt;&lt;/i&gt;&lt;/div&gt;
&lt;pre class="csharpcode"&gt;[DataContract]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Employee
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Employee(&lt;span class="kwrd"&gt;string&lt;/span&gt; name, &lt;span class="kwrd"&gt;string&lt;/span&gt; email, &lt;span class="kwrd"&gt;string&lt;/span&gt; position, &lt;span class="kwrd"&gt;int&lt;/span&gt; age)
    {
        Name = name;
        Email = email;
        Position = position;
        Age = age;            
    }
    [DataMember]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    [DataMember]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Email { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    [DataMember]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Position { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
    [DataMember]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Age { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }
}&lt;/pre&gt;
We must state &lt;i&gt;DataContract&lt;/i&gt; attribute on our &lt;i&gt;Employee &lt;/i&gt;class because we are going to send employee objects to the client.
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;u&gt;IService.cs:&lt;/u&gt;&lt;/i&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;[ServiceContract]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IService
{     
    [OperationContract]
    List&amp;lt;Employee&amp;gt; GetEmployees();     
}&lt;/pre&gt;
&lt;i&gt;&lt;u&gt;Service.svc.cs:&lt;/u&gt;&lt;/i&gt;&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Service : IService
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; List&amp;lt;Employee&amp;gt; GetEmployees()
    {
        List&amp;lt;Employee&amp;gt; employees = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Employee&amp;gt;();
        employees.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Employee(&lt;span class="str"&gt;"John"&lt;/span&gt;, &lt;span class="str"&gt;"john@codearsenal.net"&lt;/span&gt;, &lt;span class="str"&gt;"Developer"&lt;/span&gt;, 32));
        employees.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Employee(&lt;span class="str"&gt;"Smith"&lt;/span&gt;, &lt;span class="str"&gt;"smith@codearsenal.net"&lt;/span&gt;, &lt;span class="str"&gt;"Service"&lt;/span&gt;, 45));
        employees.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Employee(&lt;span class="str"&gt;"Lucas"&lt;/span&gt;, &lt;span class="str"&gt;"lucas@codearsenal.net"&lt;/span&gt;, &lt;span class="str"&gt;"Manager"&lt;/span&gt;, 25));
        employees.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Employee(&lt;span class="str"&gt;"Richard"&lt;/span&gt;, &lt;span class="str"&gt;"richard@codearsenal.net"&lt;/span&gt;, &lt;span class="str"&gt;"IT"&lt;/span&gt;, 31));
        employees.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Employee(&lt;span class="str"&gt;"Anna"&lt;/span&gt;, &lt;span class="str"&gt;"anna@codearsenal.net"&lt;/span&gt;, &lt;span class="str"&gt;"HR"&lt;/span&gt;, 28));
        &lt;span class="kwrd"&gt;return&lt;/span&gt; employees;
    }
}&lt;/pre&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Middle */
google_ad_slot = "2969384752";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
Ok, the service is ready to send employee list to the client. Now let's create our WPF client that will connect to the service and retrieve the data. Add new WPF project to the solution:&lt;/div&gt;
&lt;br/&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-mWZ6Q0yRG3U/UGMH0Ekf0bI/AAAAAAAACac/JcXSAP3VSQQ/s1600/Add-new-project-WPF-client.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Add New Project - WPF Client" border="0" height="190" src="http://4.bp.blogspot.com/-mWZ6Q0yRG3U/UGMH0Ekf0bI/AAAAAAAACac/JcXSAP3VSQQ/s320/Add-new-project-WPF-client.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Now we need to add reference to our WCF service:&lt;/div&gt;
&lt;br/&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-ibb1n8fA0yc/UGMKVB1w23I/AAAAAAAACak/AJsSSYYGrKs/s1600/add-service-reference.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Add Service Reference" border="0" height="260" src="http://4.bp.blogspot.com/-ibb1n8fA0yc/UGMKVB1w23I/AAAAAAAACak/AJsSSYYGrKs/s320/add-service-reference.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
The last is to add some code that will retrieve the data from the service and bind that data to the DataGrid.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;u&gt;&lt;i&gt;ViewModel.cs:&lt;/i&gt;&lt;/u&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ViewModel
{
    WCFServiceReference.ServiceClient _serviceClient = &lt;span class="kwrd"&gt;new&lt;/span&gt; ServiceClient();        

    &lt;span class="kwrd"&gt;public&lt;/span&gt; List&amp;lt;Employee&amp;gt; Employees
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; 
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; _serviceClient.GetEmployees().ToList(); 
        }            
    }
}&lt;/pre&gt;
&lt;br /&gt;
&lt;u&gt;&lt;i&gt;MainWindow.xaml:&lt;/i&gt;&lt;/u&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataGrid&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="dataGrid1"&lt;/span&gt; &lt;span class="attr"&gt;ItemsSource&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Employees}"&lt;/span&gt; &lt;span class="attr"&gt;AutoGenerateColumns&lt;/span&gt;&lt;span class="kwrd"&gt;="False"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataGrid.Columns&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataGridTextColumn&lt;/span&gt; &lt;span class="attr"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;="Name"&lt;/span&gt; &lt;span class="attr"&gt;Binding&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Name}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataGridTextColumn&lt;/span&gt; &lt;span class="attr"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;="Email"&lt;/span&gt; &lt;span class="attr"&gt;Binding&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Email}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataGridTextColumn&lt;/span&gt; &lt;span class="attr"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;="Position"&lt;/span&gt; &lt;span class="attr"&gt;Binding&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Position}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataGridTextColumn&lt;/span&gt; &lt;span class="attr"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;="Age"&lt;/span&gt; &lt;span class="attr"&gt;Binding&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Age}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DataGrid.Columns&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DataGrid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
This is what we'll see once we build and run the project:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-Rc6rdexdYEc/UGMN-r_oe5I/AAAAAAAACa8/HUdOpAACEfM/s1600/WPF-result.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="WPF Client window showing result in datagrid" border="0" src="http://2.bp.blogspot.com/-Rc6rdexdYEc/UGMN-r_oe5I/AAAAAAAACa8/HUdOpAACEfM/s1600/WPF-result.JPG" title="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://www.mediafire.com/?ps9uya1183qc38u" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2010 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/AbLgdtdjQEQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/8795968005413637858/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/09/wcf-service-and-wpf-client-datagrid.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8795968005413637858?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8795968005413637858?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/AbLgdtdjQEQ/wcf-service-and-wpf-client-datagrid.html" title="WCF Service Returns List of Objects to WPF DataGrid" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-ZwrDTh08yqM/UGL84Bt6CxI/AAAAAAAACaE/bnyYVtl9T48/s72-c/Add-new-project-WCF-service.JPG" height="72" width="72" /><thr:total>3</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/09/wcf-service-and-wpf-client-datagrid.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUYBQnk7cCp7ImA9WhJbF04.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-5133578217946136729</id><published>2012-09-22T12:13:00.001+03:00</published><updated>2012-09-27T10:05:53.708+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-09-27T10:05:53.708+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Events" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><title>C# Custom Event Handling</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
There are a lot of examples on Event Handling and delegates in C# on the net, but most of them are just too complicated and confusing for a beginner. Today I'd like to share with you simple and clear example of event handling with custom event arguments.&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
Imaging that we have a simple &lt;i&gt;Employee&lt;/i&gt; class. There are two properties - &lt;i&gt;Name&lt;/i&gt; and &lt;i&gt;Salary&lt;/i&gt;. We want a mechanism that will indicate that employee's salary has changed.Obviously we need create an event inside our &lt;i&gt;Employee&lt;/i&gt; class and fire it each time when salary has changed.&lt;/div&gt;
Let's see &lt;i&gt;Employee&lt;/i&gt; class source code:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Employee
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PropertyChangedHandler(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, PropertyChangeEventArgs args);

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; PropertyChangedHandler PropertyChange;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnPropertyChange(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, PropertyChangeEventArgs args)
    {
        &lt;span class="rem"&gt;// If there exist any subscribers call the event&lt;/span&gt;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (PropertyChange != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        {                
            PropertyChange(&lt;span class="kwrd"&gt;this&lt;/span&gt;, args);
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; _salary = 5000;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Salary
    {
        &lt;span class="kwrd"&gt;get&lt;/span&gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; _salary; }
        &lt;span class="kwrd"&gt;set&lt;/span&gt;
        {
            &lt;span class="kwrd"&gt;int&lt;/span&gt; oldValue = _salary;
            _salary = value;
            OnPropertyChange(&lt;span class="kwrd"&gt;this&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; PropertyChangeEventArgs(&lt;span class="str"&gt;"Salary"&lt;/span&gt;, value - oldValue));
        }
    }
}&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;i&gt;PropertyChange&lt;/i&gt; is actually our event. &lt;i&gt;PropertyChangeHandler&lt;/i&gt; delegate will allow us to subscribe to the event from other classes. We also want to pass custom arguments to subscriber, namely the difference between old amount of salary and the new one. That's why second parameter of the delegate is not &lt;i&gt;EventArgs&lt;/i&gt;, as usual, but our custom class &lt;i&gt;PropertyChangeEventArgs&lt;/i&gt;.
&lt;/div&gt;
Let's see &lt;i&gt;PropertyChangeEventArgs&lt;/i&gt; class source code:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; PropertyChangeEventArgs : EventArgs
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; PropertyName { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }        
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Difference { &lt;span class="kwrd"&gt;get&lt;/span&gt;; &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;set&lt;/span&gt;; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; PropertyChangeEventArgs(&lt;span class="kwrd"&gt;string&lt;/span&gt; propertyName, &lt;span class="kwrd"&gt;int&lt;/span&gt; difference)
    {
        PropertyName = propertyName;            
        Difference = difference;
    }
}&lt;/pre&gt;
Now let's see how to subscribe to our event from other class:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Program
{
    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
    {
        Employee employee = &lt;span class="kwrd"&gt;new&lt;/span&gt; Employee();
        employee.PropertyChange += &lt;span class="kwrd"&gt;new&lt;/span&gt; Employee.PropertyChangedHandler(PropertyChanged);
        employee.Salary = 8000;            
        Console.ReadLine();
    }
    
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PropertyChanged(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, PropertyChangeEventArgs args)
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (args.PropertyName == &lt;span class="str"&gt;"Salary"&lt;/span&gt;)
        {
            Console.WriteLine(&lt;span class="str"&gt;"Salary amount has changed! Salary difference is : "&lt;/span&gt; 
                               + args.Difference.ToString());
        }
    }
}&lt;/pre&gt;
Here is the output:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-6MJtLS6FOt8/UF1_d5HOZ6I/AAAAAAAACZs/Op9qK2_HotM/s1600/Output.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Event Handling example output" border="0" height="100" src="http://4.bp.blogspot.com/-6MJtLS6FOt8/UF1_d5HOZ6I/AAAAAAAACZs/Op9qK2_HotM/s400/Output.JPG" title="" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://www.mediafire.com/?unifqzahuu3ebad" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2010 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/CVD65dYhkg8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/5133578217946136729/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/09/csharp-custom-event-handling.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/5133578217946136729?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/5133578217946136729?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/CVD65dYhkg8/csharp-custom-event-handling.html" title="C# Custom Event Handling" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-6MJtLS6FOt8/UF1_d5HOZ6I/AAAAAAAACZs/Op9qK2_HotM/s72-c/Output.JPG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/09/csharp-custom-event-handling.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0AFSXw_fSp7ImA9WhBWGU8.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-3293250274488437520</id><published>2012-09-18T11:53:00.001+03:00</published><updated>2013-04-14T10:48:38.245+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-04-14T10:48:38.245+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Reflection" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>C# Get Assembly Location Path</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;div style="text-align: justify;"&gt;
Often when developing different applications or services you need to know where is actually your &lt;i&gt;exe&lt;/i&gt; or a &lt;i&gt;dll&lt;/i&gt; located. For example, you developing a service which will create some files and directories in the same place where the service is running, so you need to know somehow in the run time the path to your service &lt;i&gt;dll&lt;/i&gt;.&amp;nbsp;&lt;span style="text-align: left;"&gt;Fortunately for these purposes we have Reflection mechanism in C#.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="text-align: left;"&gt;First, include reflection library in your code:&lt;/span&gt;&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Reflection;&lt;/pre&gt;
&lt;br /&gt;
Now, to get the path to the assembly in the run time - use this command:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;string&lt;/span&gt; assemblyPath = Assembly.GetAssembly(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(ClassName)).Location;&lt;/pre&gt;
&lt;br /&gt;
Or if you need the path to the assembly directory then you can apply this code:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;string&lt;/span&gt; directoryPath = Path.GetDirectoryName(Assembly.GetAssembly(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(ClassName)).Location);&lt;/pre&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/p_FQZ2OlC3w" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/3293250274488437520/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/09/csharp-assembly-location-path.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/3293250274488437520?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/3293250274488437520?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/p_FQZ2OlC3w/csharp-assembly-location-path.html" title="C# Get Assembly Location Path" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/09/csharp-assembly-location-path.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkcDQH86fip7ImA9WhJVEUg.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-3848546176374722093</id><published>2012-08-28T14:38:00.001+03:00</published><updated>2012-08-28T14:41:11.116+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-08-28T14:41:11.116+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="DataTable" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="DataView" /><title>C# DataTable Order By Column</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
Sometimes when you work with DataSet or DataTable you need to order the data by specific column. The quickest way to sort DataTable by specific column is to convert it to DataView, apply the Sort method and then save back the results to your DataTable. Here is the code sample:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;//   Create DataTable and fill out with data&lt;/span&gt;
DataTable table = &lt;span class="kwrd"&gt;new&lt;/span&gt; DataTable();  
table.Columns.Add(&lt;span class="str"&gt;"EmployeeFirstName"&lt;/span&gt;, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt;));  
table.Columns.Add(&lt;span class="str"&gt;"EmployeeFamilyName"&lt;/span&gt;, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt;));  
table.Columns.Add(&lt;span class="str"&gt;"Age"&lt;/span&gt;, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;int&lt;/span&gt;));
DataRow row = table.NewRow();
  
row[&lt;span class="str"&gt;"EmployeeFirstName"&lt;/span&gt;] = &lt;span class="str"&gt;"Bill"&lt;/span&gt;;  
row[&lt;span class="str"&gt;"EmployeeFamilyName"&lt;/span&gt;] = &lt;span class="str"&gt;"Gates"&lt;/span&gt;;  
row[&lt;span class="str"&gt;"Age"&lt;/span&gt;] = 56;  
table.Rows.Add(row);  

row = table.NewRow();  
row[&lt;span class="str"&gt;"EmployeeFirstName"&lt;/span&gt;] = &lt;span class="str"&gt;"Steve"&lt;/span&gt;;  
row[&lt;span class="str"&gt;"EmployeeFamilyName"&lt;/span&gt;] = &lt;span class="str"&gt;"Ballmer"&lt;/span&gt;;  
row[&lt;span class="str"&gt;"Age"&lt;/span&gt;] = 57;  
table.Rows.Add(row); 

row = table.NewRow();  
row[&lt;span class="str"&gt;"EmployeeFirstName"&lt;/span&gt;] = &lt;span class="str"&gt;"Paul"&lt;/span&gt;;  
row[&lt;span class="str"&gt;"EmployeeFamilyName"&lt;/span&gt;] = &lt;span class="str"&gt;"Allen"&lt;/span&gt;;  
row[&lt;span class="str"&gt;"Age"&lt;/span&gt;] = 59;  
table.Rows.Add(row); 

&lt;span class="rem"&gt;//  In real life applications, it is critical &lt;/span&gt;
&lt;span class="rem"&gt;//  to check if your DataTable is not empty!&lt;/span&gt;
&lt;span class="kwrd"&gt;if&lt;/span&gt; (table.Rows.Count &amp;gt; 0)  
{
    &lt;span class="rem"&gt;//  Convert DataTable to DataView&lt;/span&gt;
   DataView dv = table.DefaultView;
   &lt;span class="rem"&gt;//   Sort data&lt;/span&gt;
   dv.Sort = &lt;span class="str"&gt;"Age"&lt;/span&gt;;
   &lt;span class="rem"&gt;//   Convert back your sorted DataView to DataTable&lt;/span&gt;
   table = dv.ToTable();  
}&lt;/pre&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/hYSJysOnHYs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/3848546176374722093/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/08/c-sharp-datatable-order-by-column.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/3848546176374722093?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/3848546176374722093?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/hYSJysOnHYs/c-sharp-datatable-order-by-column.html" title="C# DataTable Order By Column" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/08/c-sharp-datatable-order-by-column.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQHRX09cCp7ImA9WhJWFk4.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-388690761139060011</id><published>2012-08-22T15:23:00.000+03:00</published><updated>2012-08-22T15:25:34.368+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-08-22T15:25:34.368+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><category scheme="http://www.blogger.com/atom/ns#" term="XAML" /><category scheme="http://www.blogger.com/atom/ns#" term="ScrollViewer" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><title>WPF ScrollViewer Control Example</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
There are two controls that enables scrolling in WPF:&amp;nbsp;&lt;i&gt;ScrollBar&lt;/i&gt; and &lt;i&gt;ScrollViewer&lt;/i&gt;. The &lt;i&gt;ScrollViewer&lt;/i&gt;&amp;nbsp;encapsulates both vertical and horizontal &lt;i&gt;ScrollBar&lt;/i&gt;&amp;nbsp;and a container that contains all the&amp;nbsp;visible elements inside scrollable area. I'll show a &lt;i&gt;ScrollBar&lt;/i&gt; example based on code described in my &lt;a href="http://www.codearsenal.net/2012/08/wpf-expander-with-datagrid.html"&gt;previous post&lt;/a&gt;.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-bebLsFKXJ-A/UDTGo4oBlGI/AAAAAAAACXE/tDdcI3zZq9o/s1600/WPF-ScrollViewer-example.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="WPF ScrollViewer example" border="0" src="http://2.bp.blogspot.com/-bebLsFKXJ-A/UDTGo4oBlGI/AAAAAAAACXE/tDdcI3zZq9o/s1600/WPF-ScrollViewer-example.PNG" title="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;Let's see the XAML:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ScrollViewer&lt;/span&gt; &lt;span class="attr"&gt;HorizontalScrollBarVisibility&lt;/span&gt;&lt;span class="kwrd"&gt;="Auto"&lt;/span&gt; 
                &lt;span class="attr"&gt;VerticalScrollBarVisibility&lt;/span&gt;&lt;span class="kwrd"&gt;="Auto"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;               
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Expander&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="MyExpander"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="10,10"&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Expander.Content&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataGrid&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="dataGrid1"&lt;/span&gt;
                &lt;span class="attr"&gt;Cursor&lt;/span&gt;&lt;span class="kwrd"&gt;="Hand"&lt;/span&gt;
                &lt;span class="attr"&gt;ItemsSource&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Employees}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Expander.Content&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Expander.Style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Style&lt;/span&gt; &lt;span class="attr"&gt;TargetType&lt;/span&gt;&lt;span class="kwrd"&gt;="Expander"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="IsExpanded"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="False"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Header"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Click to See More"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Style.Triggers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataTrigger&lt;/span&gt; &lt;span class="attr"&gt;Binding&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding IsExpanded,RelativeSource={RelativeSource Self}}"&lt;/span&gt;                                 &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Header"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Click to See Less"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DataTrigger&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Style.Triggers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Expander.Style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Expander&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;                
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ScrollViewer&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
By specifying HorizontalScrollBarVisibility and VerticalScrollBarVisibility as "Auto" we achieve that ScrollBar becomes visible only when content becomes larger than window frame.&lt;/div&gt;
&lt;a href="http://www.mediafire.com/?snrgy8ym7qaf2xr" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2010 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/_1yLMivZZwk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/388690761139060011/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/08/wpf-scrollviewer-control-example.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/388690761139060011?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/388690761139060011?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/_1yLMivZZwk/wpf-scrollviewer-control-example.html" title="WPF ScrollViewer Control Example" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-bebLsFKXJ-A/UDTGo4oBlGI/AAAAAAAACXE/tDdcI3zZq9o/s72-c/WPF-ScrollViewer-example.PNG" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/08/wpf-scrollviewer-control-example.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D08EQX4_cSp7ImA9WhJWFUk.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-7716521009791429914</id><published>2012-08-21T13:34:00.000+03:00</published><updated>2012-08-21T13:43:20.049+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-08-21T13:43:20.049+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGrid" /><category scheme="http://www.blogger.com/atom/ns#" term="Expander" /><category scheme="http://www.blogger.com/atom/ns#" term="XAML" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><title>WPF Expander with DataGrid</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
WPF has a very useful control called &lt;i&gt;Expander. Expander&lt;/i&gt;&amp;nbsp;allows you to hide or show any content that is inside of it. Today I'll show you how to use &lt;i&gt;Expander&lt;/i&gt;&amp;nbsp;with DataGrid and&amp;nbsp;how to apply a Style to Expander's Header.&lt;/div&gt;
&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-7qRXW92_xJI/UDNOis7P9-I/AAAAAAAACVs/GSvNZHHkWGw/s1600/wpf-expander-click-to-see-more.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img alt="Collapsed WPF Expander with style and DataGrid inside of it" border="0" height="220" src="http://3.bp.blogspot.com/-7qRXW92_xJI/UDNOis7P9-I/AAAAAAAACVs/GSvNZHHkWGw/s320/wpf-expander-click-to-see-more.PNG" title="" width="320" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Collapsed state&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-st33NxeNSwg/UDNOdu2dhQI/AAAAAAAACVk/q9kxSxAhSGc/s1600/wpf-expander-click-to-see-less.PNG" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img alt="Expanded WPF Expander with style and DataGrid inside of it" border="0" height="223" src="http://3.bp.blogspot.com/-st33NxeNSwg/UDNOdu2dhQI/AAAAAAAACVk/q9kxSxAhSGc/s320/wpf-expander-click-to-see-less.PNG" title="" width="320" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Expanded state&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;span style="text-align: justify;"&gt;In this particular example I put a &lt;/span&gt;&lt;i style="text-align: justify;"&gt;DataGrid&lt;/i&gt;&lt;span style="text-align: justify;"&gt; inside of &lt;/span&gt;&lt;i style="text-align: justify;"&gt;Expander&lt;/i&gt;&lt;span style="text-align: justify;"&gt;. I also applied a &lt;/span&gt;&lt;i style="text-align: justify;"&gt;Style&lt;/i&gt;&lt;span style="text-align: justify;"&gt; to &lt;/span&gt;&lt;i style="text-align: justify;"&gt;Expander Header&lt;/i&gt;&lt;span style="text-align: justify;"&gt;. The &lt;/span&gt;&lt;i style="text-align: justify;"&gt;Style&lt;/i&gt;&lt;span style="text-align: justify;"&gt;&amp;nbsp;actually changes Header's text from "Click to See More" to "Click to See Less" when clicking on collapsed &lt;/span&gt;&lt;i style="text-align: justify;"&gt;Expander&lt;/i&gt;&lt;span style="text-align: justify;"&gt; and vice versa. Let's see the XAML:&lt;/span&gt;&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;       
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Expander&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="MyExpander"&lt;/span&gt; &lt;span class="attr"&gt;Margin&lt;/span&gt;&lt;span class="kwrd"&gt;="10,10"&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Expander.Content&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataGrid&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="dataGrid1"&lt;/span&gt; &lt;span class="attr"&gt;Cursor&lt;/span&gt;&lt;span class="kwrd"&gt;="Hand"&lt;/span&gt; &lt;span class="attr"&gt;ItemsSource&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding Employees}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Expander.Content&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Expander.Style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Style&lt;/span&gt; &lt;span class="attr"&gt;TargetType&lt;/span&gt;&lt;span class="kwrd"&gt;="Expander"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="IsExpanded"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="False"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Header"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Click to See More"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Style.Triggers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataTrigger&lt;/span&gt; &lt;span class="attr"&gt;Binding&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding IsExpanded,RelativeSource={RelativeSource Self}}"&lt;/span&gt;                                 &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Header"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Click to See Less"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DataTrigger&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Style.Triggers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Expander.Style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Expander&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;StackPanel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://www.mediafire.com/?lhdiryj1ds2tciu" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; (Visual Studio 2010 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/GXWqq6MyYUI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/7716521009791429914/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/08/wpf-expander-with-datagrid.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/7716521009791429914?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/7716521009791429914?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/GXWqq6MyYUI/wpf-expander-with-datagrid.html" title="WPF Expander with DataGrid" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-7qRXW92_xJI/UDNOis7P9-I/AAAAAAAACVs/GSvNZHHkWGw/s72-c/wpf-expander-click-to-see-more.PNG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/08/wpf-expander-with-datagrid.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0QBSH4-eyp7ImA9WhNWEUk.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-400292022070055210</id><published>2012-08-11T22:07:00.001+03:00</published><updated>2012-12-10T15:15:59.053+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-12-10T15:15:59.053+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Logging" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>C# Write to Windows Event Log</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
As you probably know Windows has its own Event Logging System. You can see the logs in it if you'll run Event Viewer from Administrative Tools. Today I'll show how to write messages into Windows Event Log from your application using C#.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-y1JsLbKHEJo/UCZI_xZSfnI/AAAAAAAACUw/FwApoPZdkXM/s1600/Event-viewer.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Microsoft Windows Event Viewer" border="0" height="149" src="http://3.bp.blogspot.com/-y1JsLbKHEJo/UCZI_xZSfnI/AAAAAAAACUw/FwApoPZdkXM/s320/Event-viewer.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;In order to write to Event Log you have to include &lt;i&gt;System&lt;/i&gt; and &lt;i&gt;System.Diagnostics&lt;/i&gt;&amp;nbsp;namespaces in your code.
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;   using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;   using&lt;/span&gt; System.Diagnostics;&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
The log message that will be inserted to event log consists of three things - the message itself, the name of the log to which you want to write your message (if such log does not exist it will be created) and a string that represents the source of your event. If you need to write messages to more than one log you must define multiple sources.
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;   string&lt;/span&gt; _source = &lt;span class="str"&gt;"My Application"&lt;/span&gt;;
&lt;span class="kwrd"&gt;   string&lt;/span&gt; _log = &lt;span class="str"&gt;"Application"&lt;/span&gt;;
&lt;span class="kwrd"&gt;   string&lt;/span&gt; _event = &lt;span class="str"&gt;"My Application Event"&lt;/span&gt;;&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
You have to check if the source that you specified really exists, if it doesn't then create it. If the log name that you specified does not exist, then it will be created automatically when you write your first message.
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;   if&lt;/span&gt; (!EventLog.SourceExists(_source))
       EventLog.CreateEventSource(_source, _log);&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
In order to write a message use EventLog.WriteEntry static method. WriteEntry has several overloaded versions, here is the simplest one:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;   EventLog.WriteEntry(_source, _event);&lt;/pre&gt;
Run the following code, then go to the Event Viewer and check your logs.
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Diagnostics;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; WriteToAnEventLog_csharp
{    
    &lt;span class="kwrd"&gt;class&lt;/span&gt; MyEvent
    {
        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
        {
            &lt;span class="kwrd"&gt;string&lt;/span&gt; _source = &lt;span class="str"&gt;"My Application"&lt;/span&gt;;
            &lt;span class="kwrd"&gt;string&lt;/span&gt; _log = &lt;span class="str"&gt;"Application"&lt;/span&gt;;
            &lt;span class="kwrd"&gt;string&lt;/span&gt; _event = &lt;span class="str"&gt;"My Application Event"&lt;/span&gt;;

            &lt;span class="kwrd"&gt;if&lt;/span&gt; (!EventLog.SourceExists(_source))
                EventLog.CreateEventSource(_source, _log);

            EventLog.WriteEntry(_source, _event);
            EventLog.WriteEntry(_source, _event, EventLogEntryType.Warning, 234);
        }
    }
}&lt;/pre&gt;
Good luck!
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/Mp8fYSmHI1s" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/400292022070055210/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/08/write-to-windows-event-log-using-c-sharp.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/400292022070055210?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/400292022070055210?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/Mp8fYSmHI1s/write-to-windows-event-log-using-c-sharp.html" title="C# Write to Windows Event Log" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-y1JsLbKHEJo/UCZI_xZSfnI/AAAAAAAACUw/FwApoPZdkXM/s72-c/Event-viewer.JPG" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/08/write-to-windows-event-log-using-c-sharp.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEcCQ3k9fyp7ImA9WhNXF00.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-8631143305948990699</id><published>2012-08-11T11:51:00.002+03:00</published><updated>2012-12-05T13:14:22.767+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-12-05T13:14:22.767+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><title>C# LINQ - OrderBy clause</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;i&gt;This post is part of a series called &lt;a href="http://www.codearsenal.net/search/label/LINQ"&gt;LINQ Examples&lt;/a&gt; that brings practical examples of using LINQ in C#.
&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;
Today we'll see practical examples of using &lt;i&gt;OrderBy&lt;/i&gt;, &lt;i&gt;ThenBy&lt;/i&gt;&amp;nbsp;clauses in LINQ. This kind of operators in LINQ are called &lt;i&gt;&lt;b&gt;LINQ&amp;nbsp;Ordering Operators&lt;/b&gt;&lt;/i&gt;.
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;List&amp;lt;Employee&amp;gt; employees = GetEmployeeList();

&lt;span class="kwrd"&gt;var&lt;/span&gt; sortedEmployees =
    &lt;span class="kwrd"&gt;from&lt;/span&gt; e &lt;span class="kwrd"&gt;in&lt;/span&gt; employees
    orderby e.EmployeeName
    select e;
Console.WriteLine(&lt;span class="str"&gt;"Employees list ordered by name:"&lt;/span&gt;);
&lt;span class="kwrd"&gt;foreach&lt;/span&gt;(&lt;span class="kwrd"&gt;var&lt;/span&gt; e &lt;span class="kwrd"&gt;in&lt;/span&gt; sortedEmployees)
    Console.Write(e.EmployeeName + &lt;span class="str"&gt;", "&lt;/span&gt;);    &lt;span class="rem"&gt;/*    Output:    John, Lucas, Marina, Susan, Tomas   */&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;We can also order our objects in opposite way simply by adding &lt;i&gt;descending&lt;/i&gt; keyword:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; sortedEmployees =
                &lt;span class="kwrd"&gt;from&lt;/span&gt; e &lt;span class="kwrd"&gt;in&lt;/span&gt; employees
                orderby e.EmployeeName descending
                select e;&lt;/pre&gt;
If we need to sort the employees by name and then by country they living in, we do it this way:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;List&amp;lt;Employee&amp;gt; employees = GetEmployeeList();

&lt;span class="kwrd"&gt;var&lt;/span&gt; sortedEmployees =
        &lt;span class="kwrd"&gt;from&lt;/span&gt; e &lt;span class="kwrd"&gt;in&lt;/span&gt; employees
        orderby e.EmployeeName, e.EmployeeCountry
        select e;            
&lt;span class="kwrd"&gt;foreach&lt;/span&gt;(&lt;span class="kwrd"&gt;var&lt;/span&gt; e &lt;span class="kwrd"&gt;in&lt;/span&gt; sortedEmployees)
    Console.WriteLine(e.EmployeeName + &lt;span class="str"&gt;" ("&lt;/span&gt; + e.EmployeeCountry + &lt;span class="str"&gt;")"&lt;/span&gt;);    
&lt;span class="rem"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/*  Output:&lt;/span&gt;
&lt;span class="rem"&gt; * &lt;/span&gt;
&lt;span class="rem"&gt; *  John (USA)                &lt;/span&gt;
&lt;span class="rem"&gt; *  Lucas (England)                &lt;/span&gt;
&lt;span class="rem"&gt; *  Marina (Russia)                &lt;/span&gt;
&lt;span class="rem"&gt; *  Susan (England)&lt;/span&gt;
&lt;span class="rem"&gt; *  Tomas (USA)   &lt;/span&gt;
&lt;span class="rem"&gt; */&lt;/span&gt;&lt;/pre&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/dBbSfp2qKhU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/8631143305948990699/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/08/c-sharp-linq-orderby-clause.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8631143305948990699?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8631143305948990699?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/dBbSfp2qKhU/c-sharp-linq-orderby-clause.html" title="C# LINQ - OrderBy clause" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/08/c-sharp-linq-orderby-clause.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0cEQn4zeip7ImA9WhJUFUw.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-8156288987695402758</id><published>2012-08-04T11:02:00.000+03:00</published><updated>2012-09-13T07:36:43.082+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-09-13T07:36:43.082+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="LINQ to SQL" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Entity Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL" /><category scheme="http://www.blogger.com/atom/ns#" term="Source Code" /><title>C# Entity Framework in Visual Studio 2010</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
&lt;/script&gt;
&lt;/div&gt;
&lt;span style="text-align: justify;"&gt;Some time ago I published an article about &lt;/span&gt;&lt;a href="http://www.codearsenal.net/2012/06/linq-to-sql-classes.html" style="text-align: justify;"&gt;LINQ to SQL Classes Generation in Visual Studio 2010&lt;/a&gt;&lt;span style="text-align: justify;"&gt;. Today I'll show how to use a newer version of Microsoft ADO.NET technology called Entity Framework.&lt;/span&gt;&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
ADO.NET Entity Framework enables developers to work directly with object model of a database instead of writing SQL queries. Developers which &amp;nbsp;already worked with LINQ to SQL will easily switch to Entity Framework, because the overall using of it is pretty similar.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Ok, let's move on to the example. I created a WPF project with a datagrid in the main window and a simple local database with one single table Employees. Now I want to use ADO.NET Entity Framework to fetch data from the database and show it in my datagrid. Final result will look like this:&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-2UtEx0gbK-c/UBzO7W20X0I/AAAAAAAACSY/1t09FvsVPto/s1600/WPF-Entity-Framework-Employees-result.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="C# WPF Datagrid with ADO.NET Entity Framework" border="0" height="207" src="http://4.bp.blogspot.com/-2UtEx0gbK-c/UBzO7W20X0I/AAAAAAAACSY/1t09FvsVPto/s320/WPF-Entity-Framework-Employees-result.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;span style="text-align: justify;"&gt;In order to add Entity Framework to your application mouse right click on the project then &lt;/span&gt;&lt;i style="text-align: justify;"&gt;"Add New Item"&lt;/i&gt;&lt;span style="text-align: justify;"&gt;, choose &lt;/span&gt;&lt;i style="text-align: justify;"&gt;ADO.NET Entity Data Model&lt;/i&gt;&lt;span style="text-align: justify;"&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-uPQiye6VLSY/UBzQ1E2YZOI/AAAAAAAACSg/9dsECyOo-9A/s1600/Visual-Studio-2010-Add-NewItem-ado.net-entity-data-model.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Add New Item ADO.NET Entity Data Model" border="0" height="221" src="http://2.bp.blogspot.com/-uPQiye6VLSY/UBzQ1E2YZOI/AAAAAAAACSg/9dsECyOo-9A/s320/Visual-Studio-2010-Add-NewItem-ado.net-entity-data-model.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
Then Entity Data Model Wizard will appear, choose &lt;i&gt;"Generate from database"&lt;/i&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-MGs4rOhG8l4/UBzRrDZUd9I/AAAAAAAACSo/LICybfq7b30/s1600/Visual-Studio-2010-ado.net-entity-data-model-generate-from-db.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="ADO.NET Data Model Wizard - Generate from database" border="0" height="285" src="http://1.bp.blogspot.com/-MGs4rOhG8l4/UBzRrDZUd9I/AAAAAAAACSo/LICybfq7b30/s320/Visual-Studio-2010-ado.net-entity-data-model-generate-from-db.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Middle */
google_ad_slot = "2969384752";
google_ad_width = 728;
google_ad_height = 90;
//&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
Click &lt;i&gt;"Next"&lt;/i&gt;, then choose your data connection. In my case this is my local DB.sdf:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-GA7mmDdGUmY/UBzU3oRmGpI/AAAAAAAACTE/hw9JEvNTsX0/s1600/entity-data-model-wizard-choose-connection.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="ADO.NET Data Model Wizard - Choose your data connection" border="0" height="283" src="http://4.bp.blogspot.com/-GA7mmDdGUmY/UBzU3oRmGpI/AAAAAAAACTE/hw9JEvNTsX0/s320/entity-data-model-wizard-choose-connection.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
Click &lt;i&gt;"Next"&lt;/i&gt;, then choose desired tables or/and stored procedures. In my case I choose my &lt;i&gt;Employee&lt;/i&gt;&amp;nbsp;table, click &lt;i&gt;"Finish"&lt;/i&gt;:&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-tMHwliv_9Xo/UBzSOmJ6v7I/AAAAAAAACSw/5xIcP6hWhD8/s1600/entity-data-model-wizard-choose-database-objects.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="ADO.NET Entity Framework - Choose your database objects" border="0" height="282" src="http://1.bp.blogspot.com/-tMHwliv_9Xo/UBzSOmJ6v7I/AAAAAAAACSw/5xIcP6hWhD8/s320/entity-data-model-wizard-choose-database-objects.JPG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
That's all! You now have Entity Framework Model. Here how you use it in the code:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; List&amp;lt;Employee&amp;gt; GetData
{
    &lt;span class="kwrd"&gt;get&lt;/span&gt;
    {
        &lt;span class="kwrd"&gt;using&lt;/span&gt; (DBEntities context = &lt;span class="kwrd"&gt;new&lt;/span&gt; DBEntities())
        {
            &lt;span class="kwrd"&gt;var&lt;/span&gt; query = &lt;span class="kwrd"&gt;from&lt;/span&gt; e &lt;span class="kwrd"&gt;in&lt;/span&gt; context.Employees
                        select e;
            &lt;span class="kwrd"&gt;return&lt;/span&gt; query.ToList&amp;lt;Employee&amp;gt;();
        }                
    }
}&lt;/pre&gt;
&lt;a href="http://www.mediafire.com/?r2waxlw06tmj2o6" rel="nofollow" target="_blank"&gt;Download the source code&lt;/a&gt; of this example (Visual Studio 2010 project).
&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//&lt;/script&gt;
&lt;/div&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/U-h_omWRcBU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/8156288987695402758/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/08/c-sharp-entity-framework-in-vs-2010.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8156288987695402758?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/8156288987695402758?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/U-h_omWRcBU/c-sharp-entity-framework-in-vs-2010.html" title="C# Entity Framework in Visual Studio 2010" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-2UtEx0gbK-c/UBzO7W20X0I/AAAAAAAACSY/1t09FvsVPto/s72-c/WPF-Entity-Framework-Employees-result.JPG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/08/c-sharp-entity-framework-in-vs-2010.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D04DQ385eCp7ImA9WhNXF00.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-6719058875322427624</id><published>2012-07-30T14:46:00.003+03:00</published><updated>2012-12-05T13:12:52.120+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-12-05T13:12:52.120+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><title>C# LINQ - Take, Skip, TakeWhile, SkipWhile clauses</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;i&gt;This post is part of a series called &lt;a href="http://www.codearsenal.net/search/label/LINQ"&gt;LINQ Examples&lt;/a&gt; that brings practical examples of using LINQ in C#.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
Today we'll see practical examples of using &lt;i&gt;Take&lt;/i&gt;, &lt;i&gt;TakeWhile&lt;/i&gt;, Skip and &lt;i&gt;SkipWhile&lt;/i&gt; clauses in LINQ. This kind of operators are called &lt;i&gt;&lt;b&gt;LINQ Partitioning Operators&lt;/b&gt;&lt;/i&gt;.
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = { 1, 3, 9, 8, 6, 7, 2, 0, 5, 10 };

&lt;span class="kwrd"&gt;var&lt;/span&gt; firstFive = numbers.Take(5);
Console.WriteLine(&lt;span class="str"&gt;"First five numbers:"&lt;/span&gt;);
&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;var&lt;/span&gt; x &lt;span class="kwrd"&gt;in&lt;/span&gt; firstFive)
    Console.Write(x + &lt;span class="str"&gt;", "&lt;/span&gt;);    &lt;span class="rem"&gt;/*    Output:    1, 3, 9, 8, 6    */&lt;/span&gt;

&lt;span class="kwrd"&gt;var&lt;/span&gt; skipFive = numbers.Skip(5);
Console.WriteLine(&lt;span class="str"&gt;"All but first five numbers:"&lt;/span&gt;); 
&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;var&lt;/span&gt; x &lt;span class="kwrd"&gt;in&lt;/span&gt; skipFive)
    Console.Write(x + &lt;span class="str"&gt;", "&lt;/span&gt;);    &lt;span class="rem"&gt;/*    Output:    7, 2, 0, 5, 10    */&lt;/span&gt;

&lt;span class="kwrd"&gt;var&lt;/span&gt; firstLessThanFive = numbers.TakeWhile(n =&amp;gt; n &amp;lt; 5);
Console.WriteLine(&lt;span class="str"&gt;"First numbers less than five:"&lt;/span&gt;); 
&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;var&lt;/span&gt; x &lt;span class="kwrd"&gt;in&lt;/span&gt; firstLessThanFive)
    Console.Write(x + &lt;span class="str"&gt;", "&lt;/span&gt;);    &lt;span class="rem"&gt;/*    Output:    1, 3    */&lt;/span&gt;

&lt;span class="kwrd"&gt;var&lt;/span&gt; fromFirstEven = numbers.SkipWhile(n =&amp;gt; n % 2 != 0);
Console.WriteLine(&lt;span class="str"&gt;"All elements starting from first even element:"&lt;/span&gt;);
&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;var&lt;/span&gt; x &lt;span class="kwrd"&gt;in&lt;/span&gt; fromFirstEven)
    Console.Write(x + &lt;span class="str"&gt;", "&lt;/span&gt;);    &lt;span class="rem"&gt;/*    Output:    8, 6, 7, 2, 0, 5, 10    */&lt;/span&gt;&lt;/pre&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/80PGE37w71g" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/6719058875322427624/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/07/c-sharp-linq-take-skip-takewhile.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/6719058875322427624?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/6719058875322427624?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/80PGE37w71g/c-sharp-linq-take-skip-takewhile.html" title="C# LINQ - Take, Skip, TakeWhile, SkipWhile clauses" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/07/c-sharp-linq-take-skip-takewhile.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0cGQn0-cCp7ImA9WhJQFEU.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-1860979378371847173</id><published>2012-07-28T18:06:00.000+03:00</published><updated>2012-07-28T18:23:43.358+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-28T18:23:43.358+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGrid" /><category scheme="http://www.blogger.com/atom/ns#" term="WPF Styles" /><category scheme="http://www.blogger.com/atom/ns#" term="XAML" /><title>WPF DataGrid ColumnHeaderStyle (LinearGradientBrush)</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
&lt;div style="text-align: justify;"&gt;
&lt;i&gt;This post is part of series called &lt;a href="http://www.codearsenal.net/search/label/WPF%20Styles" style="color: #949494; text-decoration: none;"&gt;WPF Styles&lt;/a&gt; where you can find many different styles for your WPF application.
&lt;/i&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
Due to several requests to share XAML code of the &lt;i&gt;DataGrid&lt;/i&gt; style that been used in my examples, today I'm sharing with you a &lt;i&gt;ColumnHeaderStyle&lt;/i&gt;. Just to remind, this is how the &lt;i&gt;DataGrid&lt;/i&gt; looks like:
&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-N0NgPQ86fMY/UBP848kRlaI/AAAAAAAACSA/aUzOxLCCKOY/s1600/wpf-datagrid-columnheaderstyle.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="WPF DataGrid ColumnHeaderStyle LinearGradientBrush" border="0" height="124" src="http://4.bp.blogspot.com/-N0NgPQ86fMY/UBP848kRlaI/AAAAAAAACSA/aUzOxLCCKOY/s320/wpf-datagrid-columnheaderstyle.PNG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;
So the &lt;i&gt;ColumnHeaderStyle&lt;/i&gt;&amp;nbsp;for this &lt;i&gt;DataGrid&lt;/i&gt; is following:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Style&lt;/span&gt; &lt;span class="attr"&gt;x:Key&lt;/span&gt;&lt;span class="kwrd"&gt;="HeaderStyle"&lt;/span&gt; &lt;span class="attr"&gt;TargetType&lt;/span&gt;&lt;span class="kwrd"&gt;="DataGridColumnHeader"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Background"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="{StaticResource HeaderBrush}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Foreground"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="White"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="BorderBrush"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="{StaticResource HeaderBorderBrush}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="BorderThickness"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="SnapsToDevicePixels"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="HorizontalContentAlignment"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Center"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="MinWidth"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="MinHeight"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="30"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Cursor"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Hand"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;LinearGradientBrush&lt;/span&gt; &lt;span class="attr"&gt;x:Key&lt;/span&gt;&lt;span class="kwrd"&gt;="HeaderBrush"&lt;/span&gt; &lt;span class="attr"&gt;StartPoint&lt;/span&gt;&lt;span class="kwrd"&gt;="0.5,0"&lt;/span&gt; &lt;span class="attr"&gt;EndPoint&lt;/span&gt;&lt;span class="kwrd"&gt;="0.5,1"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;GradientStop&lt;/span&gt; &lt;span class="attr"&gt;Color&lt;/span&gt;&lt;span class="kwrd"&gt;="#FF6B8E95"&lt;/span&gt; &lt;span class="attr"&gt;Offset&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;GradientStop&lt;/span&gt; &lt;span class="attr"&gt;Color&lt;/span&gt;&lt;span class="kwrd"&gt;="#FF14A7C1"&lt;/span&gt; &lt;span class="attr"&gt;Offset&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;GradientStop&lt;/span&gt; &lt;span class="attr"&gt;Color&lt;/span&gt;&lt;span class="kwrd"&gt;="#FF1E424E"&lt;/span&gt; &lt;span class="attr"&gt;Offset&lt;/span&gt;&lt;span class="kwrd"&gt;="0.509"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;GradientStop&lt;/span&gt; &lt;span class="attr"&gt;Color&lt;/span&gt;&lt;span class="kwrd"&gt;="#FF1D4855"&lt;/span&gt; &lt;span class="attr"&gt;Offset&lt;/span&gt;&lt;span class="kwrd"&gt;="0.542"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;GradientStop&lt;/span&gt; &lt;span class="attr"&gt;Color&lt;/span&gt;&lt;span class="kwrd"&gt;="#FF1D4855"&lt;/span&gt; &lt;span class="attr"&gt;Offset&lt;/span&gt;&lt;span class="kwrd"&gt;="0.542"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;GradientStop&lt;/span&gt; &lt;span class="attr"&gt;Color&lt;/span&gt;&lt;span class="kwrd"&gt;="#FF193A44"&lt;/span&gt; &lt;span class="attr"&gt;Offset&lt;/span&gt;&lt;span class="kwrd"&gt;="0.526"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;LinearGradientBrush&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;LinearGradientBrush&lt;/span&gt; &lt;span class="attr"&gt;x:Key&lt;/span&gt;&lt;span class="kwrd"&gt;="HeaderBorderBrush"&lt;/span&gt; &lt;span class="attr"&gt;StartPoint&lt;/span&gt;&lt;span class="kwrd"&gt;="0.5,0"&lt;/span&gt; &lt;span class="attr"&gt;EndPoint&lt;/span&gt;&lt;span class="kwrd"&gt;="0.5,1"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;GradientStop&lt;/span&gt; &lt;span class="attr"&gt;Color&lt;/span&gt;&lt;span class="kwrd"&gt;="#FF1D1D1D"&lt;/span&gt; &lt;span class="attr"&gt;Offset&lt;/span&gt;&lt;span class="kwrd"&gt;="0.614"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;GradientStop&lt;/span&gt; &lt;span class="attr"&gt;Color&lt;/span&gt;&lt;span class="kwrd"&gt;="#FF007F96"&lt;/span&gt; &lt;span class="attr"&gt;Offset&lt;/span&gt;&lt;span class="kwrd"&gt;="0.853"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;GradientStop&lt;/span&gt; &lt;span class="attr"&gt;Color&lt;/span&gt;&lt;span class="kwrd"&gt;="#FF0AEAFA"&lt;/span&gt; &lt;span class="attr"&gt;Offset&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;LinearGradientBrush&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
&lt;i&gt;LinearGradientBrush&lt;/i&gt; adds some fancy look to the column headers. To apply the style for your &lt;i&gt;DataGrid&lt;/i&gt; just write following:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataGrid&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="dataGrid1"&lt;/span&gt; &lt;span class="attr"&gt;ColumnHeaderStyle&lt;/span&gt;&lt;span class="kwrd"&gt;="{StaticResource HeaderStyle}"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/ICzY1CDfSMY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/1860979378371847173/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/07/wpf-datagrid-columnheaderstyle.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/1860979378371847173?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/1860979378371847173?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/ICzY1CDfSMY/wpf-datagrid-columnheaderstyle.html" title="WPF DataGrid ColumnHeaderStyle (LinearGradientBrush)" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-N0NgPQ86fMY/UBP848kRlaI/AAAAAAAACSA/aUzOxLCCKOY/s72-c/wpf-datagrid-columnheaderstyle.PNG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/07/wpf-datagrid-columnheaderstyle.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0QFRXw6fCp7ImA9WhJQFEU.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-7696361544926731889</id><published>2012-07-28T16:41:00.000+03:00</published><updated>2012-07-28T18:28:34.214+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-28T18:28:34.214+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGrid" /><category scheme="http://www.blogger.com/atom/ns#" term="WPF Styles" /><category scheme="http://www.blogger.com/atom/ns#" term="XAML" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGridRow" /><title>WPF DataGrid RowStyle (AlternationIndex)</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
&lt;i&gt;This post is part of&amp;nbsp;series called &lt;a href="http://www.codearsenal.net/search/label/WPF%20Styles"&gt;WPF Styles&lt;/a&gt;&amp;nbsp;where you can find many&amp;nbsp;different&amp;nbsp;styles for your WPF application.&lt;/i&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Today I will share with you a WPF &lt;i&gt;DataGrid RowStyle&lt;/i&gt; that I used recently for my DataGrid. At some point of development I noticed that data inside my DataGrid was really hard to read because of each row had the same color, so I decided to apply a style that will alternate the color for the rows, just like we usually see in excel reports. After applying this style your DataGrid will look like this:
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-cgMm4iCtXrA/UBPmxN6c2DI/AAAAAAAACRs/Z5dVdLLJ8zk/s1600/WPF-datagridrow-alternation-style.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="WPF DatagridRow style with AlternationIndex set to 1" border="0" height="151" src="http://3.bp.blogspot.com/-cgMm4iCtXrA/UBPmxN6c2DI/AAAAAAAACRs/Z5dVdLLJ8zk/s320/WPF-datagridrow-alternation-style.png" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;span style="background-color: white; text-align: justify;"&gt;To achieve this we need to add a trigger to our style with &lt;/span&gt;&lt;i style="background-color: white; text-align: justify;"&gt;AlternationIndex&lt;/i&gt;&lt;span style="background-color: white; text-align: justify;"&gt; set to 1. Here is the XAML of the style:&lt;/span&gt;&lt;br /&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Style&lt;/span&gt; &lt;span class="attr"&gt;x:Key&lt;/span&gt;&lt;span class="kwrd"&gt;="RowStyleWithAlternation"&lt;/span&gt; &lt;span class="attr"&gt;TargetType&lt;/span&gt;&lt;span class="kwrd"&gt;="DataGridRow"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="SnapsToDevicePixels"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Background"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="GhostWhite"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="FontWeight"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Normal"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="ContextMenu"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="{x:Null}"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Style.Triggers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Trigger&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="AlternationIndex"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="1"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Background"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="#FFD0D0E0"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Trigger&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;        
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Trigger&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="IsMouseOver"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Background"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="#F9F99F"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Trigger&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Trigger&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="IsSelected"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Background"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="#F9F99F"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Trigger&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Trigger&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Validation.HasError"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Effect"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter.Value&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DropShadowEffect&lt;/span&gt; &lt;span class="attr"&gt;Color&lt;/span&gt;&lt;span class="kwrd"&gt;="Red"&lt;/span&gt; &lt;span class="attr"&gt;ShadowDepth&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;BlurRadius&lt;/span&gt;&lt;span class="kwrd"&gt;="20"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Setter.Value&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="BorderThickness"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="2"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="BorderBrush"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Red"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Foreground"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Blue"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="FontSize"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="12"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Trigger&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Style.Triggers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
This &lt;i&gt;RowStyle&lt;/i&gt; will make your &lt;i&gt;DataGrid&lt;/i&gt; look clearly and easier to read. Just don't forget to apply the style in the &lt;i&gt;DataGrid&lt;/i&gt; definition:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataGrid&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="dataGrid1"&lt;/span&gt; &lt;span class="attr"&gt;RowStyle&lt;/span&gt;&lt;span class="kwrd"&gt;="{StaticResource RowStyleWithAlternation}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/uBJ0YmhgnvY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/7696361544926731889/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/07/wpf-datagrid-rowstyle-alternationindex.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/7696361544926731889?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/7696361544926731889?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/uBJ0YmhgnvY/wpf-datagrid-rowstyle-alternationindex.html" title="WPF DataGrid RowStyle (AlternationIndex)" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-cgMm4iCtXrA/UBPmxN6c2DI/AAAAAAAACRs/Z5dVdLLJ8zk/s72-c/WPF-datagridrow-alternation-style.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/07/wpf-datagrid-rowstyle-alternationindex.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUICQHYzfSp7ImA9WhJQEk0.&quot;"><id>tag:blogger.com,1999:blog-7986679425286454345.post-3367351373095134285</id><published>2012-07-24T17:31:00.000+03:00</published><updated>2012-07-25T11:06:01.885+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-25T11:06:01.885+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><category scheme="http://www.blogger.com/atom/ns#" term="DataGrid" /><category scheme="http://www.blogger.com/atom/ns#" term="WPF Styles" /><category scheme="http://www.blogger.com/atom/ns#" term="XAML" /><title>WPF DataGrid CellStyle (CenterCellStyle)</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Up */
google_ad_slot = "4602852119";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
&lt;i&gt;This post is part of&amp;nbsp;series called &lt;a href="http://www.codearsenal.net/search/label/WPF%20Styles"&gt;WPF Styles&lt;/a&gt;&amp;nbsp;where you can find many&amp;nbsp;different&amp;nbsp;styles for your WPF application.&lt;/i&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
When your DataGrid contains many different controls, the height of a cell in DataGrid increases accordingly. This make your text to look ugly inside the cell.&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-tFtNLfeXPcc/UA-g9RYoNwI/AAAAAAAAAH0/PJCaqtSJC9s/s1600/DataGridCell-without-style.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="DataGrid without CellStyle" border="0" height="93" src="http://4.bp.blogspot.com/-tFtNLfeXPcc/UA-g9RYoNwI/AAAAAAAAAH0/PJCaqtSJC9s/s320/DataGridCell-without-style.PNG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
As you can see "Imaging" and "Devices" are not aligned to the center of the cell and overall look of DataGrid is ugly. To fix this problem you can apply a simple style that will put the cell content at the middle of the cell.&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-SNoqV0Vjz7U/UA-hzv9qxbI/AAAAAAAAAIE/NMqwokgMyKc/s1600/DataGridCell-with-style.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="DataGrid CellStyle applied (aligned to center)" border="0" height="93" src="http://1.bp.blogspot.com/-SNoqV0Vjz7U/UA-hzv9qxbI/AAAAAAAAAIE/NMqwokgMyKc/s320/DataGridCell-with-style.PNG" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Here is DataGrid CellStyle code:
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Style&lt;/span&gt; &lt;span class="attr"&gt;x:Key&lt;/span&gt;&lt;span class="kwrd"&gt;="CenterCellStyle"&lt;/span&gt; &lt;span class="attr"&gt;TargetType&lt;/span&gt;&lt;span class="kwrd"&gt;="{x:Type DataGridCell}"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Template"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter.Value&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ControlTemplate&lt;/span&gt; &lt;span class="attr"&gt;TargetType&lt;/span&gt;&lt;span class="kwrd"&gt;="{x:Type DataGridCell}"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt; &lt;span class="attr"&gt;Background&lt;/span&gt;&lt;span class="kwrd"&gt;="{TemplateBinding Background}"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ContentPresenter&lt;/span&gt; &lt;span class="attr"&gt;HorizontalAlignment&lt;/span&gt;&lt;span class="kwrd"&gt;="Center"&lt;/span&gt; 
                                      &lt;span class="attr"&gt;VerticalAlignment&lt;/span&gt;&lt;span class="kwrd"&gt;="Center"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ControlTemplate&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Setter.Value&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Style.Triggers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Trigger&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="IsSelected"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="True"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Background"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Transparent"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="BorderBrush"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Transparent"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Setter&lt;/span&gt; &lt;span class="attr"&gt;Property&lt;/span&gt;&lt;span class="kwrd"&gt;="Foreground"&lt;/span&gt; &lt;span class="attr"&gt;Value&lt;/span&gt;&lt;span class="kwrd"&gt;="Black"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Trigger&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Style.Triggers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
All you need now to apply this style to specific column or the CellStyle of the DataGrid.

&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
 font-size: small;
 color: black;
 font-family: Consolas, "Courier New", Courier, Monospace;
 background-color: #ffffff;
 /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #a31515; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
 background-color: #f4f4f4;
 width: 100%;
 margin: 0em;
}

.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;br /&gt;
&lt;pre class="csharpcode"&gt;    &lt;span class="attr"&gt;CellStyle&lt;/span&gt;&lt;span class="kwrd"&gt;="{StaticResource CenterCellStyle}"&lt;/span&gt;
&lt;/pre&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
google_ad_client = "ca-pub-7648239438261494";
/* CodeArsenal-728x90-Leaderboard_Down */
google_ad_slot = "4113276799";
google_ad_width = 728;
google_ad_height = 90;
//--&gt;
&lt;/script&gt;
&lt;script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeArsenal/~4/QtHnX7366bk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.codearsenal.net/feeds/3367351373095134285/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.codearsenal.net/2012/07/wpf-datagrid-cellstyle-centercellstyle.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/3367351373095134285?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7986679425286454345/posts/default/3367351373095134285?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeArsenal/~3/QtHnX7366bk/wpf-datagrid-cellstyle-centercellstyle.html" title="WPF DataGrid CellStyle (CenterCellStyle)" /><author><name>Yuri Bondarenko</name><uri>https://plus.google.com/101117356840637471807</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-lnGyUFqchUE/AAAAAAAAAAI/AAAAAAAADT8/HKz-3zRITr8/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-tFtNLfeXPcc/UA-g9RYoNwI/AAAAAAAAAH0/PJCaqtSJC9s/s72-c/DataGridCell-without-style.PNG" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.codearsenal.net/2012/07/wpf-datagrid-cellstyle-centercellstyle.html</feedburner:origLink></entry></feed>
