UWP, Apply a converter to a ListView's item when the ListView's items source is a collection of strings -


i have following situation. want show list of strings. in order achieve that, have listview bound collection of strings. in collection, there empty strings. want show following text when empty string present: "-empty-". got far (the source code demonstration purpose only):

emptystringconverter.cs

public class emptystringconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, string language)     {         if (value string && string.isnullorwhitespace((string)value))         {             return "-empty-";         }         return value;     }      public object convertback(object value, type targettype, object parameter, string language)     {         throw new notimplementedexception();     } } 

mainpage.xaml

<page         x:class="app1.mainpage"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:local="using:app1"         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"         mc:ignorable="d">      <page.resources>         <local:emptystringconverter x:key="emptystringconverter" />     </page.resources>      <grid background="{themeresource applicationpagebackgroundthemebrush}">          <listview x:name="listview">             <listview.itemtemplate>                 <datatemplate>                     <textblock text="{binding converter={staticresource emptystringconverter}}" margin="0,0,0,5" />                 </datatemplate>             </listview.itemtemplate>         </listview>     </grid> </page> 

mainpage.xaml.cs

public sealed partial class mainpage : page {     public mainpage()     {         this.initializecomponent();         var source = new[] { "", "string 1", "string 2" };         listview.itemssource = source;     } } 

when put breakpoint in convert method in emptystringconverter class, method called in every single item except in empty string. how can achieve want?

well issue in place i'd check last. it's legacy binding that's causing issue. tried code snippet myself , replaced yours piece piece. below line uses legacy binding

text="{binding converter={staticresource emptystringconverter}}"

since using uwp can switch compile time binding that'll fix problem modified listview xaml be:

<listview x:name="listview" >         <listview.itemtemplate>             <datatemplate x:datatype="x:string">                 <textblock text="{x:bind converter={staticresource emptystringconverter},mode=onetime}" margin="0,0,0,5" />             </datatemplate>         </listview.itemtemplate> </listview> 

please pay attention @ 2 things:

  1. the text section of textbox in datatemplate, uses x:bind instead of binding. please note compile time binding onetime binding default (unless explicitly mention mode) legacy binding oneway binding default. more information on compile time binding follow: xbind markup extension.
  2. if notice datatemplate declaration holds datatype property, helps compile time binder know kind of data expecting. more information on datatype follow: xbind markup extension.

all being said, highly recommend using data binding approach instead of listview.itemsource=source new compile time binding, alot of conversions handled binding engine leading less code , effort on end. i've put sample on github same can check out: emptystringdemo


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -