4

I am making a Windows 8 app in VS2012 and trying to bind a list of objects (cards) to a user control that will display two strings. Here is my code for the user control on one page:

<FlipView x:Name="cardView"/>
   <FlipView.ItemTemplate>
      <DataTemplate>
         <local:TileControl x:Name="cardTile" Width="600" Height="400" QuestionText="{Binding Value.question}" AnswerText="{Binding Value.answer}"/>
      </DataTemplate>
   </FlipView.ItemTemplate>
</FlipView>

This is set in the C# code behind:

cardView.ItemsSource = Storage.content.Decks[currentDeck].Cards;

The user control 'TileControl' control has these variables inside:

public string questionTextStr { get; set; }   
public string answerTextStr { get; set; }

public string QuestionText
{
    get
    {
        return questionTextStr;
    }
    set
    {
        questionTextStr = value;
        questionText.Text = value; //set the textbox content
    }
}

public string AnswerText
{
    get
    {
        return answerTextStr;
    }
    set
    {
        answerTextStr = value;
        answerText.Text = value; //set the textbox content
    }
}

There is an error in the Error List saying "Failed to assign to property 'Flashdeck.TileControl.AnswerText' ". And it applies to the QuestionText too. The app will compile and run but when then crash when I open the page with the user control in it. What am I doing wrong to give an error and crash? Thanks.

Edit: More info on the decks and cards class. Deck:

public class Deck
    {
        public List<Card> Cards { get; set; }
        public bool flagged { get; set; }

        public Deck()
        {
        }

        public Deck(List<Card> iCards)
        {
            Cards = iCards;
            flagged = false;
        }

        public Deck(List<Card> iCards, bool iFlag)
        {
            Cards = iCards;
            flagged = iFlag;
        }
    }

Card:

public class Card
    {
        public string question { get; set; }
        public string answer { get; set; }
        public bool flagged { get; set; }

        public Card()
        {
        }

        public Card(string iQ, string iA)
        {
            question = iQ;
            answer = iA;
            flagged = false;
        }
    }
Rob Crocombe
  • 341
  • 1
  • 4
  • 15

2 Answers2

6

Your properties have to be DependencyProperties to bind values to it:

public string QuestionText
{
    get
    {
        return (string)GetValue(QuestionTextProperty);
    }
    set
    {
        SetValue(QuestionTextProperty, value);
        questionText.Text = value; //set the textbox content
    }
}
public static DependencyProperty QuestionTextProperty = DependencyProperty.Register("QuestionText", typeof(string), typeof(Deck), new PropertyMetadata(""));

...

For this your class Deck has to inherit from DependencyObject.

EDIT: In your Card-Class is no property which is called Value, thats why Binding to Value.question wont work, try instead

<FlipView x:Name="cardView"/>
   <FlipView.ItemTemplate>
      <DataTemplate>
         <local:TileControl x:Name="cardTile" Width="600" Height="400" QuestionText="{Binding Path=question}" AnswerText="{Binding Path=answer}"/>
      </DataTemplate>
   </FlipView.ItemTemplate>
</FlipView>

and call the PropertyChanged-event in the setter of your question/answer property as described here to update the binding when the value your property changed.

Florian Gl
  • 5,984
  • 2
  • 17
  • 30
  • Do you mean the Value from the XAML? It isn't, it's just set from item source 'cardView.ItemsSource = Storage.content.Decks[currentDeck].Cards;' The Decks and Cards objects are filled with example data for now. – Rob Crocombe Oct 15 '12 at 09:56
  • Yes, but there is no property in your class "Card" which is called Value. See my edit ;) – Florian Gl Oct 15 '12 at 10:00
0

if your Decks class have question and answer attributes than create assessors for them and just assign QuestionText="{Binding Question}" and AnswerText="{Binding Answer}"

in Decks class

public string Question 
{
    get;
    set;
}

public string Answer
{
    get;
    set;
}
Mayank
  • 8,777
  • 4
  • 35
  • 60
  • My deck class is a bit more complex than that. See my post edit for the code. Thanks – Rob Crocombe Oct 15 '12 at 08:56
  • Assigning `QuestionText="{Binding question}` and `AnswerText="{Binding answer}` should work for your case if everything you posted is right. When you bind an `List`, it goes and looks at that type `T` and looks for the properties, in your case `question` and `answer`. Only thing I would suggest at this point is to have `Cards` as `ObservableCollection` than `List`. – Mayank Oct 15 '12 at 19:11