Bind List of items to source to outside of source

Viewed 47

I am trying to bind label outside CarouselView with the same Title.

     <CarouselView x:Name="CVlist" 
                      ItemsSource="{Binding Source={StaticResource vm},Path=Duas}" >
                              <CarouselView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout VerticalOptions="StartAndExpand" Padding="15,0" >
                                <Label Text="{Binding Title}" />
                                 <Label Text="{Binding Proof}" FontFamily="Arabic" />
                         </StackLayout>
                     </DataTemplate>
                </CarouselView.ItemTemplate>
            </CarouselView>
            <StackLayout  >
              <Label Text="{Binding Title}" /> /* This label should update same as inside CarouselView Label bind to Title */
            </StackLayout>

This label should be updated when I scroll to next item.

1 Answers

Use carousel view's item changed event handler to set a property that the label can bind to:

<CarouselView ... CurrentItemChanged="OnCurrentItemChanged" />

<Label Text="{Binding CurrentItemTitle}" />
private string _currentItemTitle;
public string CurrentItemTitle {
  get => _currentItemTitle;
  set {
    _currentItemTitle = value;
    OnPropertyChanged();
  }
}

void OnCurrentItemChanged(object sender, CurrentItemChangedEventArgs e)
{
  CurrentItemTitle = ((MyItemType)e.CurrentItem).Title;
}

Code not tested.

Related