簡體   English   中英

綁定到UserControl“無法分配給屬性”錯誤(Windows 8)

[英]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>

並按此處所述問題/答案屬性的設置器中調用PropertyChanged事件,以在屬性值更改時更新綁定。

如果您的Decks類具有questionanswer屬性,而不是為其創建評估器,而只分配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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM