« C# code format for blogsSilverlight Configuration (almost) like regular .NET »

Silverlight 3, databinding and anonymous types

12/03/09

Permalink 10:35:05 am, Categories: Welcome

I'm currently working on a navigation service in Silverlight in order to hook perfectly Prism with the browser's navigation utilities (back, forward and URL entry). More on that later.

In doing so, I made a view whose role is to display a series of buttons hooked to commands to form a navigation bar. Each module registered with the service would get a button to navigate to it. My service holds a collection of registered modules, but it lacked a real Command object. Instead of exposing that collection, my ViewModel then exposes an enumerable list of anonymous types enriched with a command:

        public IEnumerable CommandDefinitions
        {
            get 
            {
                foreach (var entry in this.navigationService.ModuleEntries)
                {
                    yield return new
                    {
                        Command = new ModuleNavigationCommand(this.navigationService),
                        CommandText = entry.CommandText,
                        ModuleName = entry.Name
                    };
                }
            }
        }

Unfortunately, that doesn't work. The View sees that there are 2 modules and creates 2 buttons, but the buttons have no content, and when I click them, the commands don't execute! Seems like Silverlight is unable to databind on anonymous types... (does SL4 have the same problem? WPF?)

The solution is to create an holder class with the same public interface as my anonymous type, and use it instead (yes, it *has* to be public) :

        public IEnumerable CommandDefinitions
        {
            get 
            {
                foreach (var entry in this.navigationService.ModuleEntries)
                {
                    yield return new CommandDefinition
                    {
                        Command = new ModuleNavigationCommand(this.navigationService),
                        CommandText = entry.CommandText,
                        ModuleName = entry.Name
                    };
                }
            }
        }

        public struct CommandDefinition
        {
            public ICommand Command { get; set; }
            public string CommandText { get; set; }
            public string ModuleName { get; set; }
        }

2 comments

Comment from: Jason Jarrett [Visitor] · http://staxmanade.blogspot.com
Place this in your AssemblyInfo.cs.

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Windows")]

Should work.
05/18/10 @ 15:47
Comment from: Dave [Member] Email
Ah! So anonymous types are internal by default... Something I wasn't aware of. Which probably leads to the more general statement : You can't bind to internal types.

Thanks, very helpful.
05/18/10 @ 15:58

This post has 18 feedbacks awaiting moderation...

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
September 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

Here on this blog you'll find continuous thoughts and information about Omniscient's Foundation framework.

Search

The requested Blog doesn't exist any more!

XML Feeds

powered by b2evolution