【问题标题】:Binding to a Collection of the ViewModel绑定到 ViewModel 的集合
【发布时间】:2014-10-24 19:16:14
【问题描述】:

我的 Windows Phone 8.1 项目中的绑定有问题,而不是 silverlight。 我有一个简单的 ViewModel,其中包含一个 ObservableCollection 和一个 int

如果我使用设计器创建绑定,我只能看到int 属性。

我正在尝试绑定到 ItemsControlItemsSource 属性 因此,据我所知,除了用于绑定的Collection or List 之外,它应该是。

但我只看到这个:

我错过了什么吗?

这应该是可能的。

MainPage.xaml

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Nice_Dice"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uc="using:Nice_Dice"
xmlns:vm="using:Nice_Dice.ViewModel"
x:Class="Nice_Dice.MainPage"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.DataContext>
    <vm:Dices/>
</Page.DataContext>

<ScrollViewer>
    <ItemsControl x:Name="Test" ItemsSource="HERE IS WHERE I TRY TO BIND">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>
</Page>

Dices.cs (ViewModel)

public class Dices
{
    public ObservableCollection<Outcome> Outcomes = new ObservableCollection<Outcome>();
    public IList<string> Test;
    public int MyProperty { get; set; }
    public Dices()
    {
        MakeTestData();
    }        
    public void MakeTestData()
    {
        Dice a = new Dice
        {
            Name = "D6",
            States = new ObservableCollection<State>
            {
                new State 
                {
                    Value = "1"
                },
                new State
                {
                    Value = "2"
                },
                new State
                {
                    Value = "3"
                }, 
                new State
                {
                    Value = "4"
                }, 
                new State
                {
                    Value = "5"
                }, 
                new State
                {
                    Value = "6"
                }
            }
        };
        a.States[5].Dice = a;

        for (int i = 0; i <= 99; i++)
            Outcomes.Add(a.GetOutcome());
    }
}

【问题讨论】:

    标签: c# wpf xaml mvvm windows-phone-8.1


    【解决方案1】:

    使您的收藏成为属性而不是字段。数据绑定设计器只会显示属性。

    【讨论】:

    • 是的,当然...我知道这很简单:D
    • 不只是数据绑定设计器,一般数据绑定对字段都不起作用;只有属性。
    猜你喜欢
    • 2016-10-11
    • 2013-10-11
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多