Binding to UserControl Error "Failed to Assign Property" (Windows 8)

I am making a Windows 8 application in VS2012 and am trying to associate a list of objects (maps) with a user control that will display two lines. Here is my code for a 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 TileControl user control has the following variables:

 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 list of errors: “The property“ Flashdeck.TileControl.AnswerText ”could not be set. And this also applies to QuestionText. The application will compile and run, but if it fails to open the page using the user control. What am I doing wrong to give an error and fail? Thanks.

Edit: Additional information about the class of decks and cards. 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; } } 

map:

 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; } } 
+4
source share
2 answers

Your properties must 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("")); ... 

To do this, your Deck class must inherit from DependencyObject.

EDIT: Your Card-Class does not have a property called Value, so binding to Value.question does not 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 raise the PropertyChanged event in the customizer of your question / answer property, as described here , to update the binding when the value that changed your property.

+5
source

if your Decks class has question and answer attributes, than create evaluators 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; } 
0
source

Source: https://habr.com/ru/post/1439712/


All Articles