2012-10-15 27 views
4

我正在VS2012中制作Windows 8应用程序,并尝试将对象(卡片)列表绑定到将显示两个字符串的用户控件。这里是我的用户控件的代码在一个页面上:绑定到用户控件“分配给属性失败”错误(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; 
     } 
    } 
+0

什么是“Value.question”,它在哪里定义? – Mayank

回答

0

如果您Decks类有questionanswer属性不是为他们创造评审和公正的分配QuestionText="{Binding Question}"AnswerText="{Binding Answer}"

Decks

public string Question 
{ 
    get; 
    set; 
} 

public string Answer 
{ 
    get; 
    set; 
} 
+0

我的甲板课比这更复杂一点。查看我的帖子编辑的代码。谢谢 –

+0

如果你发布的内容都是正确的,那么指定'QuestionText =“{Binding question}'和'AnswerText =”{Binding answer}'应该适用于你的案例。当你绑定一个'List '时,它会去查看那个类型'T'并查找属性,在​​你的情况下是'question'和'answer'。在这一点上,我建议的唯一事情是将'Cards'设置为'ObservableCollection '而不是'List '。 – Mayank

5

你的属性必须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("")); 

... 

对于这个类甲板有自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事件/ answer属性,如here所述,以便在属性更改时更新绑定。

+0

对不起,这没有奏效。发生同样的错误。谢谢 –

+0

您的意思是来自XAML的价值?它不是,它只是从项目源'cardView.ItemsSource = Storage.content.Decks [currentDeck] .Cards;'设置甲板和卡片对象充满了现在的示例数据。 –

+0

是的,但是你的班级没有属于“卡”的属性,这就是所谓的价值。看我的编辑;) –

相关问题