dynamic datatemplates silverlight events

Looking for information on how to hook events to DataTemplate for silverlight using code only. This will crash my browser when rendered.

private DataTemplate Create(Type type)        
  DataTemplate dt = new DataTemplate();            
  string xaml = "<DataTemplate x:Name='dt' xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:basics='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls'  >"
                + "<Border Height='135' Width='110' HorizontalAlignment='Left' VerticalAlignment='Top' Background='#7FFCFAFA' CornerRadius='5,5,5,5'>"
                + "<StackPanel Orientation='Vertical' Width='110' Height='135'>"
                + "<Image Source='{Binding Path=ThumbnailPath}' Width='64' Height='64' Margin='5'  HorizontalAlignment='Left' />"
                + "<TextBlock Text='{Binding Path=Title}' Margin='5' FontFamily='Verdana' HorizontalAlignment='Left' TextWrapping='Wrap' Foreground='#FFFAF8F8' Width='100' FontSize='9' />"
                + "<TextBlock Text='{Binding Path=FileDuration}' Margin='5' FontFamily='Verdana' HorizontalAlignment='Left' Foreground='#FFFAF8F8' Width='100' FontSize='9'/>"
                + "<StackPanel Orientation='Horizontal' Background='{x:Null}' x:Name='GridMenuBar'>"
                + "<Button ClickMode='Press' x:Name='play_buttons' HorizontalAlignment='Left' Template='{StaticResource GlassPlay}'  BorderBrush='{x:Null}' Foreground='#FFF9F7F7' Background='{x:Null}' />"
                //+ "<Button Height='Auto' HorizontalAlignment='Left' VerticalAlignment='Top' Width='Auto' Content='Button' x:Name='addPlayListItemButton' Template='{StaticResource GlassAddPlaylist}' Click='addPlayListItemButton_Click' />"
                + "</StackPanel>"
                + "</StackPanel>"
                + "</Border>"
                + "</DataTemplate>"; 
    dt = (DataTemplate)XamlReader.Load(xaml);
    return dt;


What sort of exception are you getting? You can find it by looking at the script error icon in the status bar of your browser. Unless you mean this is actually completely killing the browser process. That shouldn't happen but if it does you can attach Visual Studio to your browser process before you navigate to the page.

Now, I have a few guesses as to what the problem is. First and foremost, you are referencing some components in your template that only exist within the scope of a page. For example, your button references a static template 'GlassPlay' and it also (though commented out) refers to a handler addPlayListItemButton_Click which would reside in the code-behind. XamlReader doesn't have the context to understand where to wire those things up.

Just an inside tip, User Controls don't load themselves using XamlReader but instead load up using Application.LoadComponent. LoadComponent does have some scope but it still expects all of the markup to be loaded as a unit and expects it all to come from a resource URI (there is no way to load from a string).

Dynamic data templates are possible and even referencing resources outside of the template should be fine. I know this works in WPF and the resources are resolved when the template is added to the visual tree. I'm not positive that works in Silverlight but I'm pretty sure it does.

The click handler is your biggest problem. Don't try to assign the handler in markup. Instead, give the button a name and use FindName on the template instance after it's loaded to get a hold of the button. From there you can subscribe to the events in code.

Hope that helps.

Need Your Help

Liferay clustering in the same machine

localhost liferay jackrabbit cluster-computing

Is there a way to give different Cluster ids to Jackrabbit when running two nodes in the same machine?

In C,is casting to (void*) not needed/inadvisable for memcpy() just as it is not needed for malloc()?

c casting malloc void-pointers memcpy

I have some confusions about what I read from the following site about memcpy()(and malloc()):

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.