<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><!-- generator="wordpress/2.3.1" --><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">

<channel>
	<title>development adventures</title>
	<link>http://blog.joachim.at</link>
	<description>joachim kerschbaumer about dot &amp; net</description>
	<pubDate>Fri, 18 Dec 2009 11:17:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/DevelopmentAdventures" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="developmentadventures" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Color confusions…</title>
		<link>http://blog.joachim.at/?p=45</link>
		<comments>http://blog.joachim.at/?p=45#comments</comments>
		<pubDate>Fri, 03 Apr 2009 10:27:15 +0000</pubDate>
		<dc:creator>jkersch</dc:creator>
		
		<category><![CDATA[development]]></category>

		<category><![CDATA[fun]]></category>

		<guid isPermaLink="false">http://blog.joachim.at/?p=45</guid>
		<description><![CDATA[when i was working with WPF earlier this morning i wondered how microsoft got to the color names in their System.Windows.Media.Colors class.
At least i&#8217;m somewhat confused that Gray (#FF808080) is darker than DarkGray (#FFA9A9A9)
they probably have some really strange monitor settings over there in redmond 
]]></description>
			<content:encoded><![CDATA[<p>when i was working with WPF earlier this morning i wondered how microsoft got to the color names in their <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.colors(VS.95).aspx">System.Windows.Media.Colors</a> class.</p>
<p>At least i&#8217;m somewhat confused that Gray (<i>#FF808080</i>) is <b>darker</b> than DarkGray (<i>#FFA9A9A9</i>)</p>
<p>they probably have some really strange monitor settings over there in redmond <img src='http://blog.joachim.at/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.joachim.at/?feed=rss2&amp;p=45</wfw:commentRss>
		</item>
		<item>
		<title>Using the new WPF Ribbon with CompositeWPF</title>
		<link>http://blog.joachim.at/?p=44</link>
		<comments>http://blog.joachim.at/?p=44#comments</comments>
		<pubDate>Thu, 13 Nov 2008 16:45:22 +0000</pubDate>
		<dc:creator>jkersch</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[WPF]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[CompositeWPF]]></category>

		<category><![CDATA[Ribbon]]></category>

		<guid isPermaLink="false">http://blog.joachim.at/?p=44</guid>
		<description><![CDATA[When using the new WPF Ribbon CTP with CompositeWPF (aka Prism), you probably want your modules to fill the ribbon with tabs. To let modules add content to your shell window, you have to mark the designated areas as Region.When trying to add a Region name to the ribbon with some xaml code like this [...]]]></description>
			<content:encoded><![CDATA[<p>When using the new WPF Ribbon CTP with CompositeWPF (aka Prism), you probably want your modules to fill the ribbon with tabs. To let modules add content to your shell window, you have to mark the designated areas as Region.When trying to add a Region name to the ribbon with some xaml code like this :<br />

<pre name="code" class="xml">&lt;r:Ribbon cal:RegionManager.RegionName="ApplicationRibbon"&gt; ....&lt;/r:Ribbon&gt;</pre>
<p>you will get a KeyNotFoundException. <br />Diving a little bit deeper shows you that the RegionManager cannot find a RegionAdapter mapping for <i>Microsoft.Windows.Controls.Ribbon.Ribbon</i>.That means that we have to write a RegionAdapter for the Ribbon Control.Implementing IRegionAdapter should suffice but as we are lazy, we could just inherit from RegionAdapterBase&lt;T&gt; (where our T will be Ribbon) and we will end up with something short like this:</p>
<pre name="code" class="c-sharp">
 public class RibbonRegionAdapter : RegionAdapterBase&lt;Ribbon&gt;
    {
        protected override void Adapt(IRegion region, Ribbon regionTarget)
        {
            region.Views.CollectionChanged += delegate {
                foreach (var tab in region.Views.Cast&lt;RibbonTab&gt;())
                {
                    if (!regionTarget.Tabs.Contains(tab))
                    {
                        regionTarget.Tabs.Add(tab);
                    }
                }
            };
        }

        protected override IRegion CreateRegion()
        {
            return new SingleActiveRegion();
        }
    }
</pre>
<p>
at least we have to Register this RegionAdapter in our bootstrapper. Therefore we just override the Bootstrapper&#8217;s ConfigureRegionAdapterMapping&#8217;s method:<br />
</p>
<pre name="code" class="c-sharp">
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            var mappings = Container.TryResolve&lt;RegionAdapterMappings&gt;();
            if (mappings != null)
            {
                mappings.RegisterMapping(typeof(Ribbon), new RibbonRegionAdapter());
            }
            return mappings;
        }
</pre>
<p>Now we should be able to add RibbonTab&#8217;s to the ribbon from within our Modules using RegionManager (we can easily resolve this from our IoC Container).<br />
<br />
This is no full featured quality guide for using the Wpf Ribbon control with CompositeWpf. It just shows that writing custom region adapters is actually quite an easy thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.joachim.at/?feed=rss2&amp;p=44</wfw:commentRss>
		</item>
		<item>
		<title>Barcodes everywhere…</title>
		<link>http://blog.joachim.at/?p=43</link>
		<comments>http://blog.joachim.at/?p=43#comments</comments>
		<pubDate>Mon, 10 Nov 2008 16:41:16 +0000</pubDate>
		<dc:creator>jkersch</dc:creator>
		
		<category><![CDATA[development]]></category>

		<category><![CDATA[barcode]]></category>

		<guid isPermaLink="false">http://blog.joachim.at/?p=43</guid>
		<description><![CDATA[if you&#8217;re interested, check out our blog at lemQi. I just posted a comparison of Project Bamby compared to other (commercial) barcode recognition libraries working with image processing. Also, Sebastian posted some stuff about thresholding methods we used.
There will be much more activity over there than here in the next time, although i have some [...]]]></description>
			<content:encoded><![CDATA[<p>if you&#8217;re interested, check out our blog at <a href="http://blog.lemqi.com">lemQi</a>. I just posted a comparison of Project Bamby compared to other (commercial) barcode recognition libraries working with image processing. Also, Sebastian posted some stuff about thresholding methods we used.<br />
<br />There will be much more activity over there than here in the next time, although i have some stuff pending on my &#8220;ToPublish&#8221; list <img src='http://blog.joachim.at/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.joachim.at/?feed=rss2&amp;p=43</wfw:commentRss>
		</item>
		<item>
		<title>DateTime parsing performance</title>
		<link>http://blog.joachim.at/?p=42</link>
		<comments>http://blog.joachim.at/?p=42#comments</comments>
		<pubDate>Wed, 30 Jul 2008 09:10:35 +0000</pubDate>
		<dc:creator>jkersch</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://blog.joachim.at/?p=42</guid>
		<description><![CDATA[In one of my current projects i have to convert a lot of strings to DateTime structures. Sometimes tens of thousands datetime-string arrive at the service per second. As performance is an issue in this project, i was looking for the most performant way to do all the stuff. that&#8217;s why i put on a [...]]]></description>
			<content:encoded><![CDATA[<p>In one of my current projects i have to convert a lot of strings to DateTime structures. Sometimes tens of thousands datetime-string arrive at the service per second. As performance is an issue in this project, i was looking for the most performant way to do all the stuff. that&#8217;s why i put on a little test to evaluate performance of the different DateTime parsing methods.</p>
<p>Our 4 candidates are :
<ul>
<li>DateTime.Parse</li>
<li>DateTime.ParseExact</li>
<li>DateTime.TryParse</li>
<li>DateTime.TryParseExact</li>
</ul>
<p>I also wanted to know if there is a difference when using different IFormatProviders. To benchmark i used the InvariantCulture, a concrete CultureInfo (&#8221;de-DE&#8221; in this case as i could not see any difference between &#8220;de-DE&#8221;,&#8221;en-US&#8221;,&#8221;fr-FR&#8221;,&#8230;) and no IFormatProvider for the Parse/TryParse methods (having an IFormatProvider is mandatory for the ***Exact methods).<br />The following chart shows the result for 10.000.000 calls to every one of those methods.<br /><center> <img src="http://blog.joachim.at/images/datetimparse.png" alt="DateTime parsing results" border="0" width="520" height="311" /></center><br />I was kinda surprised that TryParse was not faster than Parse. But overall the results showed what i expected. TryParseExact is by far the fastest of them all.  So , if you care about performance and have to parse a lot of strings into a DateTime structure, use TryParseExact(). </p>
<p>Comments are welcome!</p>
<p><b>Edit:</b>I was pointed out to the System.Convert.ToDateTime() Method, but as it uses DateTime.Parse() internally, performance is the same as DateTime.Parse(), so TryParseExact is still the way to go.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.joachim.at/?feed=rss2&amp;p=42</wfw:commentRss>
		</item>
		<item>
		<title>WPF WebBrowser control issues with AllowsTransparency = True</title>
		<link>http://blog.joachim.at/?p=39</link>
		<comments>http://blog.joachim.at/?p=39#comments</comments>
		<pubDate>Mon, 07 Jul 2008 13:55:05 +0000</pubDate>
		<dc:creator>jkersch</dc:creator>
		
		<category><![CDATA[WPF]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[Webbrowser]]></category>

		<guid isPermaLink="false">http://blog.joachim.at/?p=39</guid>
		<description><![CDATA[I recently played around with the new Webbrowser control delivered with the SP1 update of .NET 3.5 . 
But when i tried to use it in a WPF Window that has its WindowStyle set to None and AllowsTransparency to True (due to some custom window drawing), i noticed that the Webbrowser control disappeared.  
The Webbrowser [...]]]></description>
			<content:encoded><![CDATA[<p>I recently played around with the new Webbrowser control delivered with the SP1 update of .NET 3.5 . <br />
But when i tried to use it in a WPF Window that has its <u>WindowStyle</u> set to None and <u>AllowsTransparency</u> to True (due to some custom window drawing), i noticed that the Webbrowser control disappeared.  </p>
<p>The Webbrowser control is just a native Win32 Hwnd that gets hosted inside some WPF content area. But it&#8217;s quite annoying that the Transparency stuff gets pass on to it. <br />I wonder how i could circumvent this limitation as i don&#8217;t need transparency in the webbrowser control itself. Its just to get some homegrown, fancy-looking window style.</p>
<p>I&#8217;m looking forward to see this fixed by Microsoft in some future release.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.joachim.at/?feed=rss2&amp;p=39</wfw:commentRss>
		</item>
		<item>
		<title>sorry for downtime</title>
		<link>http://blog.joachim.at/?p=38</link>
		<comments>http://blog.joachim.at/?p=38#comments</comments>
		<pubDate>Sat, 24 May 2008 12:43:31 +0000</pubDate>
		<dc:creator>jkersch</dc:creator>
		
		<category><![CDATA[private]]></category>

		<guid isPermaLink="false">http://blog.joachim.at/?p=38</guid>
		<description><![CDATA[due to some server change and some other problems this site was down for maybe a week or so.  It should be up again now.
]]></description>
			<content:encoded><![CDATA[<p>due to some server change and some other problems this site was down for maybe a week or so.  It should be up again now.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.joachim.at/?feed=rss2&amp;p=38</wfw:commentRss>
		</item>
		<item>
		<title>(Very) simple WPF PropertyGrid in 20 minutes</title>
		<link>http://blog.joachim.at/?p=36</link>
		<comments>http://blog.joachim.at/?p=36#comments</comments>
		<pubDate>Tue, 13 May 2008 14:29:31 +0000</pubDate>
		<dc:creator>jkersch</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[WPF]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[PropertyGrid]]></category>

		<guid isPermaLink="false">http://blog.joachim.at/?p=36</guid>
		<description><![CDATA[Everyone that has ever worked with the WinForms PropertyGrid will miss it in WPF.
Therefore i wrote this tiny turorial on how to write your own very basic PropertyGrid on top of WPF. 
Disclaimer:This is not a ready to use fully-featured control for production use. It just shows in a very simple manner how to get [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone that has ever worked with the WinForms PropertyGrid will miss it in WPF.<br />
<br />Therefore i wrote this tiny turorial on how to write your own very basic PropertyGrid on top of WPF. </p>
<p><b><font color="#ff0000">Disclaimer:</font></b>This is not a ready to use fully-featured control for production use. It just shows in a very simple manner how to get Properties and their values from objects via reflection and show them using WPF and databinding.<br />
It is by far not a replacement of commercial WPF PropertyGrids (e.g. <a href="http://www.mindscape.co.nz/products/WPFPropertygrid/">MindScape WPF PropertyGrid</a>), more a fast solution for minimal requirements.</p>
<p>This simple PropertyGrid will not contain special Editors for non trivial data types. For now we are confident with a simple TextBox for demonstration purposes.<br />
We will see that because of the automatic conversion features of wpf , we&#8217;re easily able to edit not only strings, but also int&#8217;s, double&#8217;s, Color&#8217;s, SolidColorBrush&#8217;es and so on in a simple TextBox.<br />
<br />
Our WPF PropertyGrid clone should look like this:</p>
<p><center><img src="http://blog.joachim.at/images/pg1.png"></center><br />
</p>
<ul>
<li>TextBox for searching (filtering) properties. This is especially usefule if your classes contain lots of properties</li>
<li>a panel (here StackPanel) that contains all the PropertyName/PropertyValue pairs (wrapped up in a Scrollviewer)</li>
<li>TextBox for descriptions that we obtain from the System.ComponentModel.DescriptionAttribute-Attribute of the property selected</li>
</ul>
<p>
First we want to realize the simple structure shown above in XAML to get started.<br />
After short time this may look like this:<br />
<center><img src="http://blog.joachim.at/images/pg2.png"></center><br />
<br />
Next we reate a UserControl as a Wrapper for our Properties. This just contains a TextBlock (for the property name) and a TextBox (for the property value) bound to properties that we defined in our code-behind file.<br />
<br />
Now where the actual work is done:</p>
<pre name="code" class="c-sharp">
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value)){

	if (!property.IsBrowsable) continue; //not browsable. ignore

        PropertyItem currentProperty = new PropertyItem();
        currentProperty.PropertyName = property.Name;
        Binding b = new Binding(property.Name);
        b.Source = selectedObject;
	//OneWay Binding if readonly, twoway else
        b.Mode = property.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;

        currentProperty.SetBinding(PropertyItem.PropertyValueProperty, b);
	currentProperty.OnActive += new EventHandler<DescriptionEventArgs>(currentProperty_OnActive);

        foreach (Attribute attribute in property.Attributes)	//check attributes
        {
         if (attribute.GetType() == typeof(DescriptionAttribute)){ //description to show in description textbox
            currentProperty.PropertyDescription = ((DescriptionAttribute)attribute).Description;
         }
         if (attribute.GetType() == typeof(CategoryAttribute)) {  //categories known from winforms pg.not implemented
             currentProperty.PropertyCategory = ((CategoryAttribute)attribute).Category;
         }
        }      

	PropertyPanel.Children.Add(currentProperty); //add PropertyItem to panel
}
</pre>
<p>
That&#8217;s it! Well, what happened here?<br />
We iterate over all properties the object, we set as SelectedObject, contains. If it&#8217;s marked as non-Browsable (<a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.browsableattribute.aspx">System.ComponentModel.BrowsableAttribute</a>), we&#8217;ll just skip it, otherwise we create a PropertyItem, set its name and create a Binding to connect the PropertyItem&#8217;s PropertyValue-property with the value of the actual property from SelectedObject.<br />
<br />
And before we add the PropertyItem to our Panel, we check the attributes of the source property (e.g. to get the description from the DescriptionAttribute or the get the corresponding category from the CategoryAttribute, which is not processed for simplicity in this demo).</p>
<p>When we now try to set some object to our PropertyGrid&#8217;s SelectedObject-property (in this case a dummy class containing several properties, including string, int, double, color, SolidColorBrush and a property marked with Browsable(false)) it should look like this:</p>
<p><center<<img src="http://blog.joachim.at/images/pg3.png"></center></p>
<p>In the screeshot above you can see different data types in their string representation. You don&#8217;t have to care about converting them back manually to their actual type as WPF&#8217;s Binding-Helpers do everything for you (and will throw Exceptions if the string-format for your type is wrong).</p>
<p>At last we add some Style to our PropertyGrid and received something like this:<br />
<center><img src="http://blog.joachim.at/images/pg4.png"></center><br />
<br />
To use the (very) simple PropertyGrid, just add a reference to PropertyGridDemo.dll, add the namespace in XAML to your window like this :</p>
<pre name="code" class="xml">
<Window x:Class="MyWindow"
xmlns:ctl="clr-namespace:PropertyGridDemo;assembly=PropertyGridDemo"
>
</pre>
<p>&#8230;and add the PropertyGrid itself somewhere in your XAML code</p>
<pre name="code" class="xml">
<Grid>
  <ctl:PropertyGrid x:Name="propertyGrid"/>
</Grid>
</pre>
<p>at last you have to provide some object (that contains some properties) to the SelectedObject property:</p>
<pre name="code" class="c-sharp">
//...
this.propertyGrid.SelectedObject = myCustomObject;
//...
</pre>
<p>
You can download the whole solution here: <a href="http://blog.joachim.at/download/PropertyGridDemo.zip">PropertyGridDemo.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.joachim.at/?feed=rss2&amp;p=36</wfw:commentRss>
		</item>
		<item>
		<title>NHibernate.QueryException: unexpected token: as [from mytable as m…</title>
		<link>http://blog.joachim.at/?p=34</link>
		<comments>http://blog.joachim.at/?p=34#comments</comments>
		<pubDate>Mon, 05 May 2008 07:58:37 +0000</pubDate>
		<dc:creator>jkersch</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[NHibernate]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://blog.joachim.at/?p=34</guid>
		<description><![CDATA[Today i encountered a strange behaviour with NHibernate. The error message in the headline was kinda confusing. I spent some time on google and everything i found was that my mapping is probably wrong or contains some bugs though i knew my mapping was definitely correct.
Some time later i found out what made this error [...]]]></description>
			<content:encoded><![CDATA[<p>Today i encountered a strange behaviour with NHibernate. The error message in the headline was kinda confusing. I spent some time on google and everything i found was that my mapping is probably wrong or contains some bugs though i knew my mapping was definitely correct.</p>
<p>Some time later i found out what made this error appear. When you have a lot of entities and therefore a huge mapping, it can take quite a long time to process your hbm file. When you now want to access some of your entities from another thread as the main thread via NHibernate, but NHibernate still has not finished processing your mapping, then you&#8217;ll also get the error stated above.This will remain as long as you use the same session. </p>
<p>Maybe this can save some other guy&#8217;s time <img src='http://blog.joachim.at/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.joachim.at/?feed=rss2&amp;p=34</wfw:commentRss>
		</item>
		<item>
		<title>WCF and Streaming data made easy</title>
		<link>http://blog.joachim.at/?p=33</link>
		<comments>http://blog.joachim.at/?p=33#comments</comments>
		<pubDate>Sat, 03 May 2008 07:47:01 +0000</pubDate>
		<dc:creator>jkersch</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[WCF]]></category>

		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://blog.joachim.at/?p=33</guid>
		<description><![CDATA[Wcf is great when it comes to message exchange. But Wcf even contains functionality when you want to stream data.
This is especially useful if you want to transmit a huge amount of data (think about the maxBufferSize option in Wcf configuration) or you want your clients to start processing data before the Wcf operation call [...]]]></description>
			<content:encoded><![CDATA[<p>Wcf is great when it comes to message exchange. But Wcf even contains functionality when you want to stream data.<br />
<br />This is especially useful if you want to transmit a huge amount of data (think about the maxBufferSize option in Wcf configuration) or you want your clients to start processing data before the Wcf operation call has finished.<br />
<br />Using Wcf for streaming data is incredibly easy. Let&#8217;s look at a small example. We define our ServiceContract in the same way we are used to ist except the fact that we can just return a System.IO.Stream (or inherited) object to our clients: </p>
<pre name="code" class="c-sharp">
[ServiceContract]
    public interface IStreamService
    {
        [OperationContract]
        Stream GetData(string fileName);
    }
</pre>
<p>We now want to define a service that just takes a filename as an argument and returns the data the file contains in a stream. Therefore our implementation of IStreamService is quite easy too:</p>
<pre name="code" class="c-sharp">
  public class StreamService : IStreamService
    {
        public Stream GetData(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open)
            return fs;
        }
    }
</pre>
<p>That&#8217;s everything we have to do on the service side (exception configuration). When you want to use streaming, be sure to use one of the following bindings:</p>
<ul>
<li>BasicHttpBinding</li>
<li>NetTcpBinding</li>
<li>NetNamedPipeBinding</li>
</ul>
<p>These bindings expose a Property called &#8220;TransferMode&#8221; where you can specify how you want to transmit your data. This could be either &#8220;Buffered&#8221; (default) or &#8220;Streamed&#8221; (or even StreamedResponse/StreamedRequest if you want to stream just in one direction).</p>
<p>Next just add a service reference to your client (within visual studio).You way also want to set the maxReceivedMessageSize property of your binding to a value high enough to contain your whole stream data, as everything transmitted via your stream is regarded as one huge message. But you can leave maxBufferSize to its default (64k bytes) as this indicates only the amout that is buffered before it gets processed.<br />
<br />Then we can just use the stream as we did years ago :</p>
<pre name="code" class="c-sharp">
StreamDemo.StreamServiceClient client = new WcfStreamDemoClient.StreamDemo.StreamServiceClient();
Stream str = client.GetData(@"c:\path\to\myfile.dat");
FileStream fs = new FileStream(@"c:\wcf\transmitted\copy.dat", FileMode.Create);

int b,i=0;
do
{
      b = str.ReadByte(); //read next byte from stream
      fs.WriteByte((byte)b); //write byte to local file
      i++;
} while (b != -1);
Console.Write("\rRead {0} bytes.", i);
str.Close();
fs.Close();
</pre>
<p>That&#8217;s it!<br />
I can imagine a lot of distributed applications where this could be really useful. Maybe this micro tutorial can be useful too <img src='http://blog.joachim.at/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.joachim.at/?feed=rss2&amp;p=33</wfw:commentRss>
		</item>
		<item>
		<title>WPF DataBinding and Value Formatting for fun and profit</title>
		<link>http://blog.joachim.at/?p=32</link>
		<comments>http://blog.joachim.at/?p=32#comments</comments>
		<pubDate>Fri, 25 Apr 2008 07:38:17 +0000</pubDate>
		<dc:creator>jkersch</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[WPF]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[DataBinding]]></category>

		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://blog.joachim.at/?p=32</guid>
		<description><![CDATA[WPF offers great functionality when it comes to DataBinding. You can e.g. set a List of Customer-Objects as DataContext on a ListView and don&#8217;t have to care about the rest.The following example shows a List of Customer&#8217;s bound to a ListView:

But what if you want to display some Properties of your objects in another format [...]]]></description>
			<content:encoded><![CDATA[<p>WPF offers great functionality when it comes to DataBinding. You can e.g. set a List of Customer-Objects as DataContext on a ListView and don&#8217;t have to care about the rest.The following example shows a List of Customer&#8217;s bound to a ListView:</p>
<p><img src="http://blog.joachim.at/images/converter1.png" height="196" width="342" /><br />
But what if you want to display some Properties of your objects in another format (i.e. something other that ToString() returns) ? You can either override ToString() or try to use another (imho more elegant) way.<br />
Therefore WPF offers an Interface called IValueConverter </p>
<pre name="code" class="c-sharp">
public interface IValueConverter {
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture);
   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
}
</pre>
<p>When implementing this interface in your own class, you can probably do everything you can imagine with the values you try to format. You can e.g. display the birthdate instead of its (normal) DateTime representation as Base64-string (this has no real use, it&#8217;s just an example of how far you can go). Therefore we create the following converter: </p>
<pre name="code" class="c-sharp">
public class Base64Converter : IValueConverter
{
  #region IValueConverter Members

  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
      return System.Convert.ToBase64String(Encoding.ASCII.GetBytes(value.ToString()));
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
      return Encoding.ASCII.GetString(System.Convert.FromBase64String(value.ToString()));
  }

  #endregion
}
</pre>
<p>Next we have to add our Namespace to the Window:</p>
<pre name="code" class="xml">
&lt;Window x:Class="CustomValueConverter:Window1"
. . .
xmlns:converters="clr-namespace:CustomValueConverter.Converter"&gt;
</pre>
<p>In the next step we create our Converter in our Window&#8217;s Resources:</p>
<pre name="code" class="xml">
. . .
&lt;Window.Resources&gt;
    &lt;converter:Base64Converter x:Key="base64"/&gt;
&lt;/Window.Resources&gt;
. . .
</pre>
<p>And from now on we can use our Converter everywhere we use DataBinding. For example:</p>
<pre name="code" class="xml">
. . .
&lt;TextBlock Text="{Binding Path=Birthday,Converter={StaticResource base64}}"/&gt;
. . .
</pre>
<p>would give the following result:</p>
<p><img src="http://blog.joachim.at/images/converter2.png"/></p>
<p>You can also pass parameters directly from XAML to a class implementing IValueConverter. This could be useful if you want to e.g. display some DateTime values in your own format.<br />
Therefore we change our Convert()-method in the following way:</p>
<pre name="code" class="c-sharp">

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
     return ((DateTime)value).ToString((string)parameter);
}
</pre>
<p>Now the DateTime value will be formatted corresponding to our format string we supply as a parameter from XAML. We can declare our parameters like this :</p>
<pre name="code" class="xml">
&lt;TextBlock Text="{Binding Path=Birthday,
                  Converter={StaticResource parameterized},
                  ConverterParameter='dd@MM@yyyy'}"/&gt;
</pre>
<p>what gives us the following result</p>
<p><img src="http://blog.joachim.at/images/converter3.png"></p>
<p>With ValueConverters you can transform your objects to any representation you want. You&#8217;re only limited by your imagination.<br />
The ConvertBack() method of IValueConverter is needed to convert the values from Convert() back to its original representation.<br /> This is necessary especially if you bind some values to TextBoxes (or other value-changing controls) and don&#8217;t want your user to use the original representation of your object&#8217;s values.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.joachim.at/?feed=rss2&amp;p=32</wfw:commentRss>
		</item>
	</channel>
</rss>
