【问题标题】:Binding to a UserControl "Failed to assign to property" error (Windows 8)绑定到用户控件“无法分配给属性”错误 (Windows 8)
【发布时间】:2012-10-04 23:14:44
【问题描述】:

我正在 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;
        }
    }

【问题讨论】:

  • 什么是Value.question,它在哪里定义?

标签: c# data-binding user-controls windows-8


【解决方案1】:

您的属性必须是 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 继承。

编辑:在您的 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>

并按照here 的描述在您的问题/答案属性的设置器中调用 PropertyChanged 事件,以在您的属性值更改时更新绑定。

【讨论】:

  • 您是指来自 XAML 的值吗?不是,它只是从项目源'cardView.ItemsSource = Storage.content.Decks[currentDeck].Cards;'中设置的甲板和卡片对象现在填充了示例数据。
  • 是的,但是在您的“卡片”类中没有称为值的属性。查看我的编辑;)
【解决方案2】:

如果您的Decks 类具有questionanswer 属性,则为它们创建评估者并分配QuestionText="{Binding Question}"AnswerText="{Binding Answer}"

Decks类中

public string Question 
{
    get;
    set;
}

public string Answer
{
    get;
    set;
}

【讨论】:

  • 我的套牌课比这要复杂一些。有关代码,请参阅我的帖子编辑。谢谢
  • 如果您发布的所有内容都是正确的,那么分配 QuestionText="{Binding question}AnswerText="{Binding answer} 应该适合您的情况。当您绑定List&lt;T&gt; 时,它会查看该类型T 并查找属性,在​​您的情况下为questionanswer。在这一点上我唯一建议的是将Cards 设置为ObservableCollection&lt;T&gt; 而不是List&lt;T&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 2011-09-06
相关资源
最近更新 更多