Windows Phone App 8. Can't display data in xaml markup

126 Views Asked by At

I've been able to transfer objects between pages with ease, but now I can't display the data in the xaml markup.

Here's the Quote entity that's stored in a sdf-file on the application:

[Table]
    public class Quote
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id { get; set; }


        [Column(CanBeNull = false)]
        public string QuoteOfTheDay { get; set; }


        [Column(CanBeNull = false)]
        public string SaidBy { get; set; }


        [Column(CanBeNull = true)]
        public string Context { get; set; }


        [Column(CanBeNull = true)]
        public string Episode { get; set; }


        [Column(CanBeNull = true)]
        public string Season { get; set; }
    }

Here's the code behind:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    DataContext = this;

    var quote = PhoneApplicationService.Current.State["q"];             

    Quote quoteToDisplay = (Quote)quote;       
}

public static readonly DependencyProperty QuoteToDisplayProperty = DependencyProperty.Register(
    "QuoteToDisplay", typeof(Quote), typeof(PhoneApplicationPage), new PropertyMetadata(default(Quote)));

public Quote QuouteToDisplay
{
    get { return (Quote)GetValue(QuoteToDisplayProperty); }
    set { SetValue(QuoteToDisplayProperty, value); }
}

The xaml:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <TextBlock FontSize="36" FontFamily="Verdana" FontWeight="ExtraBlack" Text="{Binding QuoteToDisplay.QuoteOfTheDay}" />
    </Grid>

I got the exact data that I want to display in the xaml. I want to display the QuoteOfTheDay property in the TextBlock. But everytime I try to use {Binding}, the TextBlock is always empty. When I also try to use Binding, intellisense doesn't suggest "QuoteOfTheDay".

I've obviuosly missed something important, but I can't really see what it is.

2

There are 2 best solutions below

0
IL_Agent On

Why do you use {Binding} if you assign .Text property in code-behind ? I think you you must remove binding from xaml or (and it's better way) use MVVM.

11
eshaham On

A quick look at you code shows several problems:

  1. You are initializing a TextBlock in your C# code, giving it the same name as the TextBlock you defined in XAML. This means you're not changing any of the properties of the XAML TextBlock which is the one that is actually shown.
  2. You are specifying a DataContext for your TextBlock to be quoteToDisplay.QuoteOfTheDay, but then your binding statement in XAML is {Binding quoteToDisplay.QuoteOfTheDay}, which means you're trying to bind into a non existing hierarchy quoteToDisplay.QuoteOfTheDay.quoteToDisplay.QuoteOfTheDay. You're probably getting a BindingExpression error in the Output windows due to this mistake.

What I would do is this:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    DataContext = this;

    var quote = PhoneApplicationService.Current.State["q"];

    QuoteToDisplay = (Quote)quote;
}

public static readonly DependencyProperty QuoteToDisplayProperty = DependencyProperty.Register(
    "QuoteToDisplay", typeof (Quote), typeof (MainPage), new PropertyMetadata(default(Quote)));

public Quote QuoteToDisplay
{
    get { return (Quote) GetValue(QuoteToDisplayProperty); }
    set { SetValue(QuoteToDisplayProperty, value); }
}

And in the XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <TextBlock FontSize="36" FontFamily="Verdana" FontWeight="ExtraBlack" Text="{Binding QuoteToDisplay.QuoteOfTheDay}" />
</Grid>