<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Listview on Ash Tewari</title><link>https://www.tewari.info/tag/listview/</link><description>Recent content in Listview on Ash Tewari</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Wed, 16 Apr 2008 05:22:12 +0000</lastBuildDate><atom:link href="https://www.tewari.info/tag/listview/feed.xml" rel="self" type="application/rss+xml"/><item><title>Using Custom Converter in XAML</title><link>https://www.tewari.info/2008/04/15/using-custom-converter-in-xaml/</link><pubDate>Wed, 16 Apr 2008 05:22:12 +0000</pubDate><guid>https://www.tewari.info/2008/04/15/using-custom-converter-in-xaml/</guid><description>&lt;p&gt;In my previous post : &lt;a class="link" href="https://www.tewari.info/2008/03/07/wpf-linq-to-sql-sample/" title="wpf-linq-to-sql-sample"&#10; &gt;WPF-LINQ To SQL Sample&lt;/a&gt;, I had to resort to modifying .dbml file to get the images stored as varbinary into the WPF ListView control. I had to change the Type of image fields to System.Byte[], instead of using System.Data.Linq.Binary. A better way to do this is to use a custom converter. WPF allows you to specify custom converter as part of the Binding in .xaml, like this -&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;Image Grid.Row=&amp;#34;2&amp;#34;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Source=&amp;#34;{Binding Path=ThumbNailPhoto ,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Converter={StaticResource imageConverter}}&amp;#34;&amp;gt;&amp;lt;/Image&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The custom converter is declared in the same .xaml file as a resource -&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;Window.Resources&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;local:ImageDataConverter x:Key=&amp;#34;imageConverter&amp;#34; /&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;/Window.Resources&amp;gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;ImageDataConverter is implemented in .xaml.cs file -&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;public class ImageDataConverter : IValueConverter&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; public object Convert(object value, Type targetType, object parameter,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; System.Globalization.CultureInfo culture)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; byte[] image = ((System.Data.Linq.Binary)value).ToArray();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; return image;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; public object ConvertBack(object value, Type targetType, object parameter,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; System.Globalization.CultureInfo culture)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; throw new NotSupportedException();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The updated sample code can be downloaded here : &lt;a class="link" href="https://www.tewari.info/downloads/Linq2SqlSample2.zip" title="Linq2SqlSample2.zip"&#10; &gt;WPF-LINQ to SQL Sample&lt;/a&gt;&#10;License : &lt;a class="link" href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" target="_blank" rel="noopener"&#10; &gt;Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.&lt;/a&gt;&lt;/p&gt;&#10;</description></item><item><title>WPF - LINQ to SQL Sample</title><link>https://www.tewari.info/2008/03/07/wpf-linq-to-sql-sample/</link><pubDate>Fri, 07 Mar 2008 07:15:35 +0000</pubDate><guid>https://www.tewari.info/2008/03/07/wpf-linq-to-sql-sample/</guid><description>&lt;p&gt;This exercise in binding a WPF ListView control with LINQ to SQL was a lot of fun ! The following image shows data from &lt;a class="link" href="http://www.codeplex.com/MSFTDBProdSamples" title="MSSQL Sample Databases"&#10; target="_blank" rel="noopener"&#10; &gt;AdventureWorks&lt;/a&gt; sample database displayed in the ListView control. The end result is very admirable, even in this very basic demo. Feeling a complete sense of freedom is unavoidable when working with WPF.&lt;/p&gt;&#10;&lt;p&gt;&lt;img alt="Linq2SqlSample1.Png" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://www.tewari.info/images/Linq2SqlSample1.Png"&gt;&lt;/p&gt;&#10;&lt;p&gt;This sample demonstrates :&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;LINQ to SQL mapping.&lt;/li&gt;&#10;&lt;li&gt;WPF ListView Control DataBinding.&lt;/li&gt;&#10;&lt;li&gt;Displaying images stored in database as varbinary in WPF control.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The sample code can be downloaded here : &lt;a class="link" href="https://www.tewari.info/downloads/Linq2SqlSample.zip" title="Linq2SqlSample.zip"&#10; &gt;WPF-LINQ to SQL Sample&lt;/a&gt;&#10;License : &lt;a class="link" href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" target="_blank" rel="noopener"&#10; &gt;Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.&lt;/a&gt; &lt;a class="link" href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" target="_blank" rel="noopener"&#10; &gt;&lt;img alt="Creative Commons License" class="gallery-image" data-flex-basis="681px" data-flex-grow="283" height="31" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="http://i.creativecommons.org/l/by-nc-sa/3.0/us/88x31.png" width="88"&gt;&lt;/a&gt;&lt;/p&gt;&#10;</description></item></channel></rss>