【问题标题】:Can't get Properties to ListView from each item in ObservableCollection无法从 ObservableCollection 中的每个项目获取 ListView 的属性
【发布时间】:2015-07-24 12:15:58
【问题描述】:

我想在 xaml 中测试 ListView,所以我决定创建一个小测验(简单的数学问题,有 3 个答案可供选择)。因此,我创建了一个问题模型、问题视图模型(具有我的模型问题的所有属性),最后是一个 ViewModelClass,其中我有可观察的视图模型问题集合。 当我将 ListView ItemSource 属性与 ObservableCollection 绑定时,我从每个项目中得到的唯一东西是 Method ToString()。我不知道如何解决这个问题。 这是我的代码:

Xaml:

<StackPanel Grid.Row="1" DataContext="{StaticResource ResourceKey=viewModel}">
        <ListView ItemsSource="{Binding Questions}" >
            <TextBlock Style="{StaticResource SubheaderTextBlockStyle}">
                <Run Text="Question "/>
                <Run Text="{Binding QuestionNumber}"/>
            </TextBlock>
            <TextBlock>
                <Run Text="How much is:  "/>
                <Run Text="{Binding First}"/>
                <Run Text=" * "/>
                <Run Text="{Binding Second}"/>
            </TextBlock>
            <!-- Here should be three answers to choose -->
        </ListView>
    </StackPanel>

我的 ViewModel 问题(我称之为问题的 ObservableColletion 中的每个项目):

    class QuestionViewModel
{
    Question myQuestion;
    int questionNumber;

    public QuestionViewModel(Question question,int number)
    {
        myQuestion=question;
        questionNumber = number;
    }

    public int First
    {
        get
        {
            return myQuestion.first;
        }
    }

    public int Second
    {
        get
        {
            return myQuestion.second;
        }
    }

    public int QuestionNumber
    {
        get
        {
            return questionNumber;
        }
    }

    public int FirstAnswer
    {
        get
        {
            return myQuestion.answers[0];
        }
    }

    public int SecondAnswer
    {
        get
        {
            return myQuestion.answers[1];
        }
    }

    public int ThirdAnswer
    {
        get
        {
            return myQuestion.answers[2];
        }
    }

}

和 ViewModelClass:

    class QuestionsViewModel
{
    public ObservableCollection<QuestionViewModel> Questions{get;private set;}

    public QuestionsViewModel()
    {
        Questions = new ObservableCollection<QuestionViewModel>();
        GenerateQuestion(10,10);
    }

    private void GenerateQuestion(int howmuch,int range)
    {
        for (int i = 0; i < howmuch; i++)
            Questions.Add(new QuestionViewModel(new Question(range),i+1));
    }

}

【问题讨论】:

  • 是的,我知道我需要将 int 转换为字符串,但我没有从我的列表中看到任何属性。
  • 您能花点时间编辑您的帖子,格式化代码块以使其更具可读性吗?

标签: c# xaml listview windows-store-apps


【解决方案1】:

ListView 不知道如何在ListViewItem 中渲染每个QuestionViewModel。这就是DataTemplate 发挥作用的地方。

您忘记为每个ListViewItem 定义一个DataTemplate。试试这个

<ListView ItemsSource="{Binding Questions}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock... //your text blocks
            </StackPanel>
        </DataTemplate> 
    </ListView.ItemTemplate>
</ListView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 2019-08-11
    • 2017-09-27
    • 1970-01-01
    • 2017-10-13
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多