Xamarin表单ListView ObservableCollection未更新

ind*_*lee 4 listview observablecollection xamarin.forms

我有一个ListView必然ObservableCollection由一个组成viewModels.

viewModel看起来像这样:

public string Id{ get; set; }
public string Name{ get; set; }
public bool HasId
{
    get { return !(String.IsNullOrEmpty(Id)); }
}
Run Code Online (Sandbox Code Playgroud)

在前端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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Kei*_*ome 6

您需要INotifyPropertyChanged在viewmodel类上实现.

ObservableCollection<T>班只处理有关收集本身是否已经改变的通知-项目添加/删除/等.它不处理有关各个项目本身的属性何时更改的通知.