views:

469

answers:

2

Hi. I have a WPF app that uses x:type when working with datatemplates. This doesn't work in Silverlight out of the box, but I can remember that I saw something some time ago in a googlegroup where they where talking about Silverlight Extensions and how that could be used.

If anyone knows what I am talking about or knows how I can reuse my datatemplates in silverlight and has some nice samplecode you would make my day.

/johan

A: 

in silverlight, you leave off the x:Type and drop the braces... like this....

<Style TargetType="local:TemplatedControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:TemplatedControl">
                    <StackPanel x:Name="Panel" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>
Muad'Dib
I'm using MMVM so I dynamically load objects and want the UI to display the right view depending on type. Does youor solution still solve my problem??
take your WPF xaml and replace the {x:Type .... }, remove the braces, remove the x:Type and you should be good to go. silverlight is a bit different, so you might have to make template selectors or such.
Muad'Dib
A: 

I had the same problem a couple of days ago... and I have found a solution... I am not very proud of it but it works.. create a datatemplate and add there all the possible usercontrols

 <DataTemplate x:Key="WorkspaceItemTemplate">
            <Grid>
                <View:TreeView Visibility="{Binding Converter={StaticResource ViewVisibilityConverter}, ConverterParameter=TreeView}" />
                <View:GridView Visibility="{Binding Converter={StaticResource ViewVisibilityConverter}, ConverterParameter=GridView}" />
                <View:DataView Visibility="{Binding Converter={StaticResource ViewVisibilityConverter}, ConverterParameter=DataView}" />
            </Grid>
        </DataTemplate>

and create a convertor that changes visibility based on type

 public class ViewVisibilityConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (((ViewModelBase)value).DisplayName.Equals((string)parameter))
                return Visibility.Visible;
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
bogdanbrudiu