![](/img/trans.png)
[英]Error binding to UserControl property if type is ImageSource in WinRT
[英]Binding to a UserControl “Failed to assign to property” error (Windows 8)
我正在VS2012中制作Windows 8應用程序,並嘗試將對象(卡片)列表綁定到將顯示兩個字符串的用戶控件。 這是我在一頁上的用戶控件的代碼:
<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>
這是在后面的C#代碼中設置的:
cardView.ItemsSource = Storage.content.Decks[currentDeck].Cards;
用戶控件“ TileControl”控件內部包含以下變量:
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
}
}
錯誤列表中顯示錯誤“無法分配給屬性'Flashdeck.TileControl.AnswerText'”。 它也適用於QuestionText。 該應用程序將編譯並運行,但隨后當我打開其中包含用戶控件的頁面時崩潰。 我做錯了什么錯誤並崩潰了? 謝謝。
編輯:甲板和卡片類的更多信息。 甲板:
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;
}
}
卡:
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;
}
}
您的屬性必須是DependencyProperties才能將值綁定到它:
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(""));
...
為此,您的Deck類必須從DependencyObject繼承。
編輯:在您的卡類中沒有屬性稱為值,這就是為什么綁定到Value.question無法正常工作,請嘗試
<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>
如果您的Decks
類具有question
和answer
屬性,而不是為其創建評估器,而只分配QuestionText="{Binding Question}"
和AnswerText="{Binding Answer}"
在Decks
課上
public string Question
{
get;
set;
}
public string Answer
{
get;
set;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.