C# “绑定到用户控件”;未能分配给属性";错误(Windows 8)
我正在VS2012中制作一个Windows 8应用程序,并尝试将对象列表(卡片)绑定到一个将显示两个字符串的用户控件。以下是我在一页上的用户控件代码:C# “绑定到用户控件”;未能分配给属性";错误(Windows 8),c#,data-binding,user-controls,windows-8,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" Ques
<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>
用户控件“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'”。这也适用于问题文本。
该应用程序将编译并运行,但当我打开包含用户控件的页面时会崩溃。我犯了什么错误导致出错和崩溃?谢谢
编辑:更多关于甲板和卡片类的信息。甲板:
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;
}
}
如果你的
deck
类有question
和answer
属性,那么为它们创建评估员,只需分配QuestionText=“{Binding question}”
和AnswerText=“{Binding answer}”
在甲板中
类
public string Question
{
get;
set;
}
public string Answer
{
get;
set;
}
您的属性必须是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继承
编辑:在你的卡片类中没有称为值的属性,这就是为什么绑定到值。问题不起作用,请尝试
<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>
并在问答属性的setter中调用PropertyChanged事件(如上所述),以便在属性的值更改时更新绑定。什么是
value.question
以及它的定义位置?My deck类比这要复杂一些。有关代码,请参见我的帖子编辑。感谢签名QuestionText=“{Binding question}
和AnswerText=“{Binding answer}
如果您发布的所有内容都正确,那么签名应该适用于您的案例。当您绑定一个列表
时,它会去查看该类型的T
,并查找属性,在您的例子中是问题
和答案
。在这一点上,我唯一建议的是将卡片
作为可观察收集
而不是列表
。您是指XAML中的值吗?不是,它只是从项目源“cardView.ItemsSource=Storage.content.Deck[currentDeck].Cards;”设置的现在,卡片组和卡片对象中填充了示例数据。是的,但是类“Card”中没有称为Value的属性。查看我的编辑;)
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(""));
...
<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>