locked
TextBlock text doesn't bind to properties in a StackPanel

    Question

  • I'm building a small Windows 8 app and after hours of banging my head, can't figure out why my Binding isn't working. I have this ViewModel:


    public class DetailsViewModel : ViewModelBase { private BookDataService _bookDataService; private Uri _largeImage;
        public Uri LargeImage
        {
            get
            {
                return _largeImage;
            }
            set
            {
                _largeImage = value;
                OnPropertyChanged("LargeImage");
            }
        }

    private string _bookTitle; public string BookTitle { get { return _bookTitle; } set { _bookTitle = value; OnPropertyChanged("BookTitle"); } } public DetailsViewModel() { _authorsBooks = new ObservableCollection<Book>();
            LargeImage = new Uri("ms-appx:///Assets/Logo.scale-100.png"); // just placeholder
            BookTitle = "LOTR"; // just placeholder } public async Task GetBookDetails(string bookID) { _bookDataService = new BookDataService(); var remoteResults = await _bookDataService.GetBookDetailsAsync(bookID); BookTitle = remoteResults.Title;
            LargeImage = remoteResults.LargeImage; } }

    This is the XAML for the StackPanel:


    <StackPanel x:Name="BookDetailsStackPanel" 
                            Grid.Row="0" 
                            Orientation="Horizontal" 
                            DataContext="{Binding DetailsViewModel}">
                    <Image Source="{Binding LargeImage}" 
                           Width="200" Height="300" 
                           VerticalAlignment="Top" 
                           HorizontalAlignment="Left"  
                           Margin="24"/>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Margin="24" Text="{Binding BookTitle}" Foreground="Black"/>
                        <TextBlock Margin="24" Text="{Binding Author.Name}" Foreground="Black"/>
                        <TextBlock Margin="24" Foreground="Black">
                            <Run Text="{Binding Rating}"/>
                            <Run Text="("/>
                            <Run Text="{Binding RatingsCount}"/>
                            <Run Text=")"/>
                        </TextBlock>
                    </StackPanel>
                </StackPanel>

    I initialize the ViewModel in the code-behind and get the data:


    private DetailsViewModel _detailsViewModel = new DetailsViewModel();
    public DetailsViewModel DetailsViewModel
    {
        get { return this._detailsViewModel; }
    }
    
    async void DetailsPage_Loaded(object sender, RoutedEventArgs e)
    {
        BookDetailsProgressRing.IsActive = true;
        await _detailsViewModel.GetBookDetails(_bookID);
        BookDetailsProgressRing.IsActive = false;
    }

    Obviously, I'm only talking about the BookTitle property, I will set the others once I know what's wrong with my code. After hours of debugging I can say for sure, that BookTitle is set to the correct value, but it just doesn't bind to the TextBlock. Also, I have a ListView on the same page, that binds to the AuthorsBooks property and it works perfectly, and the LargeImage property binds also without problems, only the TextBlocks won't show what they are supposed to. Also, when running the app, there aren't any Binding Errors showing up in the Output window. Any suggestions ?



    Sunday, March 23, 2014 12:06 PM

All replies

  • I really do not like this method

        public async Task GetBookDetails(string bookID)
        {
            _bookDataService = new BookDataService();
            var remoteResults = await _bookDataService.GetBookDetailsAsync(bookID);
    
            BookTitle = remoteResults.Title;
            LargeImage = remoteResults.LargeImage;
        }

    Why are you calling it async?  What does the task return?  Why not just change it to this

        public async void GetBookDetails(string bookID)
        {
            _bookDataService = new BookDataService();
            var remoteResults = await _bookDataService.GetBookDetailsAsync(bookID);
    
            BookTitle = remoteResults.Title;
            LargeImage = remoteResults.LargeImage;
        }



    • Edited by Ken Tucker Sunday, March 23, 2014 2:03 PM re-phased qustion
    Sunday, March 23, 2014 1:33 PM
  • the binding on the root stackpanel binds to the datacontxt of its parent. but i cant see that one inn your snippet. can you show bit more xaml? (whole page?)

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, March 24, 2014 10:43 AM