Windows Phone Silverlight - setting button IsDisabled if bound data length is 0 -


here's i've got - i'm writing app that, among other things, reads rss feed episodes of podcast, displays each episode's title , description, "listen" , "watch" button. not episodes have both options - rss return empty string instead of url either option if it's not available. i'm trying use ivalueconverter can bind isdisabled to, returns true if bound data length 0, , false otherwise. now, i'm testing on "watch" buttons, since binding identical "listen" buttons.

a snippet of mainpage.xaml.cs:

using system.xml.linq; using system.windows.data; namespace appname {     public partial class mainpage : phoneapplicationpage     {         public mainpage()         {             initializecomponent();             webclient podcastlistdownloader = new webclient();             podcastlistdownloader.downloadstringcompleted += new downloadstringcompletedeventhandler(podcastlistdownloadcompleted);             podcastlistdownloader.downloadstringasync(new uri("http://domain.tld/mobile_app/podcastfeed"));         }         void podcastlistdownloadcompleted(object sender, downloadstringcompletedeventargs e)         {             if (e.error != null)                 return;             xelement xmlpodcastlist = xelement.parse(e.result);             podcastlistbox.itemssource = podcastepisode in xmlpodcastlist.descendants("item")                                    select new podcastitem                                    {                                        title = podcastepisode.element("date").value + " " + podcastepisode.element("title").value,                                        subtitle = podcastepisode.element("subtitle").value,                                        description = podcastepisode.element("summary").value,                                        audio = podcastepisode.element("audio").value,                                        video = podcastepisode.element("video").value,                                    };         }         private void playpodcast(object sender, routedeventargs e)         {             button btn = (button)sender;             microsoft.phone.tasks.mediaplayerlauncher podcastplay = new microsoft.phone.tasks.mediaplayerlauncher();             podcastplay.media = new uri(btn.tag.tostring());             podcastplay.show();         } } public class podcastitem {     public string title { get; set; }     public string description { get; set; }     public string audio { get; set; }     public string video { get; set; }     public string subtitle { get; set; } } public class stringlengthvisibilityconverter: ivalueconverter {     public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         if (value == null || value.tostring().length == 0)         {             return false;         }         else         {             return true;         }     }     public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         throw new notimplementedexception();     } } 

a snippet of mainpage.xaml:

<phone:phoneapplicationpage  x:class="ccofnow.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:microsoft.phone.controls;assembly=microsoft.phone" xmlns:shell="clr-namespace:microsoft.phone.shell;assembly=microsoft.phone" xmlns:controls="clr-namespace:microsoft.phone.controls;assembly=microsoft.phone.controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  mc:ignorable="d" d:designwidth="480" d:designheight="800"  fontfamily="{staticresource phonefontfamilynormal}" fontsize="{staticresource phonefontsizenormal}" foreground="{staticresource phoneforegroundbrush}" supportedorientations="portrait"  orientation="portrait" shell:systemtray.isvisible="false">  <grid x:name="layoutroot" background="transparent">     <!--panorama control-->     <controls:panorama title="appname">         <controls:panorama.background>             <imagebrush imagesource="panoramabackground.png"/>         </controls:panorama.background>         <controls:panoramaitem header="podcast" foreground="{staticresource phoneaccentbrush}">             <listbox margin="0,0,-12,0" itemssource="{binding items}" name="podcastlistbox">                 <listbox.itemtemplate>                     <datatemplate>                         <stackpanel margin="0,0,0,17" width="432">                             <textblock text="{binding title}" textwrapping="wrap" style="{staticresource phonetextextralargestyle}"/>                             <textblock text="{binding description}" textwrapping="wrap" margin="12,-6,12,0" style="{staticresource phonetextsubtlestyle}"/>                             <stackpanel orientation="horizontal">                                 <button content="listen" width="215" tag="{binding audio}" click="playpodcast"/>                                 <button content="watch" width="215" tag="{binding video}" click="playpodcast" isenabled="{binding video, converter={stringlengthvisibilityconverter}}"/>                             </stackpanel>                         </stackpanel>                     </datatemplate>                 </listbox.itemtemplate>             </listbox>         </controls:panoramaitem>     </controls:panorama> </grid> </phone:phoneapplicationpage> 

but debugger throwing 2 errors:

1) tag 'stringlengthvisibilityconverter' not exist in xml namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation

2) type 'stringlengthvisibilityconverter' not found. verify not missing assembly , referenced assemblies have been built

i set converter {staticresource stringlengthvisibilityconverter} instead (per http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=vs.95).aspx), , there's 1 error: resource "stringlengthvisibilityconverter" not resolved. error, can debug (run) code, "watch" buttons remain enabled.

so i'm guessing i'm calling in wrong namespace, can't seem figure out correct one. can please point me in right direction?

thanks!

edit: in process of putting together, realized need differently - feed has additional values can databind to. however, i'm quite sure i'm going need functionality @ point in future, i'm going post anyway. if there's easy solution question, please let me know can learn , sucessfully next time!

the way reference converter isn't quite right. need instance of converter available somwhere, e.g. in page's resources section:

<phone:phoneapplicationpage xmlns:conv="namespace reference converter goes here"                                ...>     <phone:phoneapplicationpage.resources>         <conv:stringlengthvisibilityconverter x:key="length" />     </phone:phoneapplicationpage.resources> 

then reference converter using staticresource reference x:key gave converter.

<button content="watch"          width="215"         tag="{binding video}"         click="playpodcast"         isenabled="{binding video, converter={staticresource length}}"/> 

i'll leave discussion of approach versus using commands , mvvm day :)


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -