anouar.bagari
anouar.bagari

Reputation: 2104

Differences in Binding between Silverlight and WPF

I was looking for code samples of the ListPicker control In Windows Phone 7 and I ran into this one (http://www.c-sharpcorner.com/uploadfile/f397b9/using-listpicker-in-windows-phone-7/)

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Name="lpkItemTemplate">
        <TextBlock Text="{Binding Country}" />
    </DataTemplate>
    <DataTemplate x:Name="lpkFullItemTemplate">
        <TextBlock Text="{Binding Country}" />
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>
.
.
.
<toolkit:ListPicker FullModeItemTemplate="{Binding lpkFullItemTemplate}"
            Grid.Row="5" ItemTemplate="{Binding lpkItemTemplate}"
            x:Name="lpkCountry"/>

In this example the ListPicker DataTemplates are defined inside the PhoneApplicationPage.Resources dictionnary, and the properties FullModeItemTemplate and ItemTemplate are set using the {Binding} extension, I have a little background in WPF so I wasn't expecting the code to run properly but when I tried it it worked just fine, so I tried an equivalent example in WPF but did not work

<Window.Resources>
    <Style TargetType="Button" x:Name="btnComic">
        <Setter Property="FontFamily" Value="Comic Sans MS"/>
        <Setter Property="Foreground" Value="Red"/>
    </Style>
</Window.Resources>
<Grid>
    <Button Style="{Binding btnComic}" Content="Test"/>
</Grid>

Is binding in silverlight and WPF so different to the point that the same syntax works in one platform but not in the other? or I'm just missing something?

Upvotes: 1

Views: 51

Answers (1)

McGarnagle
McGarnagle

Reputation: 102753

WPF and WP Silverlight are somewhat different, but the bindings work in more or less the same way. The code at the link is gibberish. (See here for a correct example.)

What is probably happening is that the "FullModeItemTemplate" and "ItemTemplate" bindings are failing (check your Output window in VS to confirm). Notice that the DataTemplates are also nonsense (the items are strings, not objects with a "Country" property). The code probably runs because the ListPicker will display the items as strings without the item templates set.

Upvotes: 2

Related Questions