Xamarin Forms ListView ObservableCollection 未更新 [英] Xamarin Forms ListView ObservableCollection not updating

查看:26
本文介绍了Xamarin Forms ListView ObservableCollection 未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView,它绑定到一个由 viewModels 组成的 ObservableCollection.

I have a ListView that is bound to an ObservableCollection composed of viewModels.

viewModel 看起来像这样:

the viewModel looks like this:

public string Id{ get; set; }
public string Name{ get; set; }
public bool HasId
{
    get { return !(String.IsNullOrEmpty(Id)); }
}

在前端 xaml 中:

in the frontend xaml:

<ListView x:Name="MyList" ItemsSource="{Binding MyList}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout>
          <Label Text="{Binding Name" FontSize="Small"/>
          <StackLayout Spacing="20">
            <Entry Text="{Binding Id}"/>
            <Button Text="Create Profile" IsEnabled="False">
              <Button.Triggers>
                <DataTrigger TargetType="Button" Binding="{Binding HasId}" Value="True">
                  <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
              </Button.Triggers>
            </Button>
          </StackLayout>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

期望结果:当用户输入条目时(绑定到Id),按钮应该被启用.

Desired Result: when user types into entry (binded to Id), the button should be enabled.

实际结果:当用户输入条目时,没有任何反应(我猜是因为 ObservableCollection 没有看到 viewmodel 中的变化>)

Actual Result: when user types into entry, nothing happens (I'm guessing cause the ObservableCollection does not see the change within the viewmodel)

注意:当我向 ObservableCollection 添加/删除元素时,视图会更新(如预期).

Note: When I add/delete an element to/from the ObservableCollection, the view does update (as expected).

非常感谢任何建议/想法!

Any suggestions/ideas are greatly appreciated!

推荐答案

你需要在viewmodel类上实现INotifyPropertyChanged.

You need to implement INotifyPropertyChanged on the viewmodel class.

ObservableCollection 类仅处理有关集合本身是否已更改的通知 - 添加/删除/等项目.它不处理单个项目本身的属性何时更改的通知.

The ObservableCollection<T> class only handles notifications regarding whether the collection itself has changed - items added/removed/etc. It does not deal with notifications for when properties of the individual items themselves are changed.

这篇关于Xamarin Forms ListView ObservableCollection 未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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