4
votes

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

the viewModel looks like this:

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

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>

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

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

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

Any suggestions/ideas are greatly appreciated!

2
your ViewModel class needs to implement INotifyPropertyChangedJason
@Jason thanks! I didn't understand it the first time I read the documentation, but now it makes sense.indubitablee

2 Answers

6
votes

You need to implement INotifyPropertyChanged on the viewmodel class.

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.

0
votes

The Entry has a event name Called OnTextChanged (something like that, which you can use to enable the button, this can be done in Xaml or in code behind.

In regards of the listView not being refreshed have you tried to set "MyList" ObservableCollection again? which trigger the OnPropertyChanged Event of the View Model?