Oleg Kalyta
Oleg Kalyta

Reputation: 584

XAML template binding in Windows 8

I am walking through Windows Store Sample Application XAML Twitter Client 1, to get the same features in my own application. But i can't get binding work in 1 to 1 sample page.

This is my grid for displaying friends:

  <GridView x:Name="FriendsGrid" 
                  Grid.Row="2" 
                  Grid.Column="1" 
                  HorizontalAlignment="Left" 
                  VerticalAlignment="Top" 
                  Margin="10,10,0,0"
                  ItemsSource="{Binding Friends}" 
                  ItemTemplate="{StaticResource FriendItemTemplate}" 
                  Grid.ColumnSpan="2">
        <GridView.DataContext>
            <Model:FriendsViewModel/>
        </GridView.DataContext>

Template for binding:

<Page.Resources>
    <DataTemplate x:Key="FriendItemTemplate">
        <Grid Height="200" Width="300">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <Image HorizontalAlignment="Left" 
                       Height="80" Width="130" 
                       Margin="10,10,0,0" 
                       VerticalAlignment="Top" 
                       Source="{Binding RealPhoto}" 
                       Stretch="UniformToFill"/>

            <TextBlock Grid.Column="1" 
                           HorizontalAlignment="Left" 
                           Margin="10,10,0,0" 
                           TextWrapping="Wrap" 
                           VerticalAlignment="Top" 
                           Height="80" 
                           Width="130" 
                           Text="{Binding FirstName}"/>

            <TextBlock HorizontalAlignment="Left"
                           Margin="10,10,0,0" 
                           Grid.Row="1" 
                           TextWrapping="Wrap"
                           Text="{Binding LastName}"
                           VerticalAlignment="Top"
                           Width="280" Height="80"
                           Grid.ColumnSpan="2" />
        </Grid>
    </DataTemplate>
</Page.Resources>

In my code-behind file:

    private FriendsViewModel _model;

    public MyPage()
    {
        this.InitializeComponent();
        _model = new FriendsViewModel();

        FriendsGrid.DataContext = _model;
    }

Than i populate model, in application i see exactly the same items count that i`ve added, but items are empty. Using debug i see, that model is not empty. Also when i am hard-coding values in templates, they are visible.

Test project on GitHub

Upvotes: 1

Views: 1302

Answers (2)

Oleg Kalyta
Oleg Kalyta

Reputation: 584

I forgot to add getters and setters to properties of my model. Initially i had this:

public double Uid;

Than i added {get; set;}

[DataContract]
public class Friend
{
    [DataMember(Name = "uid")] 
    public double Uid { get; set; }

    [DataMember(Name="first_name")]
    public string FirstName { get; set; }

    [DataMember(Name="last_name")]
    public string LastName { get; set; }

    [DataMember(Name="online")]
    public bool Online { get; set; }

    [DataMember(Name = "photo")]
    public string Photo { get; set; }

    public ImageSource RealPhoto { get; set; }
}

Upvotes: 1

N_A
N_A

Reputation: 19897

To see changes in your view model you need to implement INotifyPropertyChanged. Part of doing this is calling NotifyPropertyChanged on a property if its value changes.

Upvotes: 0

Related Questions