views:

2985

answers:

1

Hello all,

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; }

+4  A: 

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.

Jared Bienz - MSFT
Jared, can you share a link on some tutorials for dynamic loading components in SL ?
vittore