Rob*_*mbe 4 c# data-binding user-controls 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>
Run Code Online (Sandbox Code Playgroud)
这是在C#代码后面设置的:
cardView.ItemsSource = Storage.content.Decks[currentDeck].Cards;
Run Code Online (Sandbox Code Playgroud)
用户控件'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
}
}
Run Code Online (Sandbox Code Playgroud)
错误列表中的错误说"无法分配给属性'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;
}
}
Run Code Online (Sandbox Code Playgroud)
卡:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
您的属性必须是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(""));
...
Run Code Online (Sandbox Code Playgroud)
为此,您的类Deck必须继承自DependencyObject.
编辑:在您的Card-Class中没有被称为Value的属性,这就是为什么绑定到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>
Run Code Online (Sandbox Code Playgroud)
并在此处所述的问题/答案属性的setter中调用PropertyChanged事件,以便在属性更改的值时更新绑定.
| 归档时间: |
|
| 查看次数: |
3402 次 |
| 最近记录: |