【问题标题】:can you data bind a ListBox with an IEnumerable<type>您可以将 ListBox 与 IEnumerable<type> 数据绑定吗
【发布时间】:2013-10-21 12:04:38
【问题描述】:

我有以下 xaml:

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="OrganisationTemplate">
        <Grid Margin="40,0,0,0">
            <StackPanel>
                <TextBlock Text="{Binding name}"></TextBlock>
            </StackPanel>
        </Grid>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

        <phone:PanoramaItem Header="Organisations">
            <ListBox Name="Organisation" ItemTemplate="{StaticResource OrganisationTemplate}" DataContext="{Binding Organisations, Mode=OneWay}" Margin="0,0,0,96"/>
        </phone:PanoramaItem>

我想知道绑定的“组织”是否需要是 List 或 ICollection 或其他东西......或者它可以是 IEnumerable。只是它目前不起作用。

class DashboardViewModel
{
    private OrganisationRepository organisationRepository { get; set; }
    public IEnumerable<Organisation> Organisations { get; set; }

    public DashboardViewModel()
    {
        LoadOrganisationSection();
    }

    private async void LoadOrganisationSection()
    {
        organisationRepository = new OrganisationRepository();
        Organisations = await organisationRepository.GetAll();
        OnPropertyChanged("Organisations");
        //LoadBar loadBar = new LoadBar("Logging in");
        //loadBar.ShowLoadBar(this);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler tempEvent = PropertyChanged;

        if (tempEvent != null)
        {
            // property changed
            tempEvent(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

编辑:://

    public IEnumerable<Organisation> Organisations
    {
        get
        {
            return new Organisation[] { new Organisation { name = "hi" } };
        }
        set
        {
            OnPropertyChanged();
        }
    }

如果我这样做,我会得到一些东西,所以触发器不起作用。有什么想法我该怎么做吗?当我对 organizationRepository.GetAll() 的等待完成时。我需要 onchange 发生并更新。呵呵

【问题讨论】:

    标签: c# data-binding mvvm windows-phone-8 listbox


    【解决方案1】:

    尝试将列表框绑定设置为 ItemsSource,而不是设置其数据上下文。 (假设您的视图模型已经设置为视图的数据上下文)

    【讨论】:

    • 啊,我会试试的伙伴!现在谢谢(必须等到我回家再买这个)
    • ItemsSource="{Binding Organisations, Mode=OneWay}" 不起作用。多哈。但我认为 ienumerable 不是问题
    • stackoverflow.com/questions/4176778/… 我将 Organizations = null 设置为强制更改属性 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多