数据到指定的ItemsSource新的数据之后,从的ListView消失 [英] Data disappears from ListView after assigning new data to ItemsSource

查看:541
本文介绍了数据到指定的ItemsSource新的数据之后,从的ListView消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下的的ListView

<ListView 
    Grid.Row="1" 
    ItemsSource="{Binding MeditationDiary}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Width="{Binding ElementName=ListViewHeaders, Path=ActualWidth}" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition  />
                    <ColumnDefinition  />
                </Grid.ColumnDefinitions>
                <TextBlock 
                    Grid.Column="0" 
                    Text="{Binding StartTime}" />
                <TextBlock 
                    Grid.Column="1" 
                    Text="{Binding TimeMeditated}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

这势必类型的 MeditationDiary MeditationEntries MeditationDiary 属性code>:

This is bound to a MeditationDiary property of type MeditationDiary consisting of MeditationEntries:

public class MeditationDiary : Collection<MeditationEntry> { }

public class MeditationEntry
{
    public DateTime StartTime { get; set; }
    public TimeSpan TimeMeditated { get; set; }
}

的ListView 绑定到这个 MeditationDiary

private MeditationDiary _meditationDiary;
public MeditationDiary MeditationDiary
{
    get
    {
        RaisePropertyChanged(nameof(MeditationDiary));
        return _meditationDiary;
    }
    set
    {
        _meditationDiary = value;
        RaisePropertyChanged(nameof(MeditationDiary));
    }
}

奇怪的是,当我分配一个新的 MeditationDiary 对象属性(其中包含的数据与 MeditationEntries )的的ListView 确实不再显示数据。

Strangely enough when I assign a new MeditationDiary object to the property (which contains data with MeditationEntries) the ListView does no longer display data.

我指定的 UpdateDiary 方法,新的 MeditationDiary 对象是添加条目之后调用:

I'm assigning the new MeditationDiary object in the UpdateDiary method which is called after adding an entry:

private async void UpdateDiary()
{
    var latestDiary = await _repository.GetAsync();
    MeditationDiary = latestDiary;
}

为什么会这样,以及如何可以把它固定?

Why can this be and how can it be fixed?

推荐答案

这是最有可能的是搞乱了的罪魁祸首你的绑定:

This is most likely the culprit that's messing up your binding:

MeditationDiary = latestDiary;

相反,尝试清除出当前的集合,然后添加新值吧:

Instead, try clearing out the current collection and then adding the new values to it:

MeditationDiary.Clear();

foreach (var entry in latestDiary)
    MeditationDiary.Add(entry);

您可能会拥有你添加新的项目后打电话到 RaisePropertyChanged 的集合。

You'll probably have to call RaisePropertyChanged on the collection after you add the new items.

作为一个方面说明,您可以取代 MeditationDiary 的ObservableCollection&LT; MeditationEntry&GT; ,它会自动通知用户界面当您添加/删除里面的物品。

As a side note, you could replace MeditationDiary with an ObservableCollection<MeditationEntry>, which automatically notifies the UI when you add/remove items in it.

这篇关于数据到指定的ItemsSource新的数据之后,从的ListView消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆