We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Accessing ListView item from within the ViewCell

Hello!  I am going to try to explain what I am trying to achieve and maybe you can point me in the right direction.  I have an sfListView populated with ViewCells.  Also, I have a ListViewSelectionChanged event to change the appearance of the ViewCell when it is selected.  Within the ViewCell, there is an Entry view.   I would like to change the appearance of the ViewCell (basically call ListViewSelectionChanged), when the Entry text is changed.  I assume I would need to add an myEntry.TextChanged event, but within the TextChanged event, how do I access the listview item to change it to IsSelected = true?


2 Replies

PU pumaprog July 22, 2017 07:58 PM UTC

I figured it out - it was right in front of me.  I just needed to declare my sfListView as public static sfListView listView. 



DB Dinesh Babu Yadav Syncfusion Team July 24, 2017 12:13 PM UTC

Hi Tommy, 
 
Thank you for using Syncfusion Products. 
 
You can also achieve the reported requirement “Changing the background color of View in SfListView” by using BindingContext of the Entry which will be the Model object(Example: Contacts). In the Model class, you need to have IsSelected property and bind it to the CustomView as like below code snippet. So, when the entry text is changed, set the IsSelected property as “True” and the respective CustomView’s converter will be triggered and based on the IsSelected value, you can change the appearance of the view. In the sample, we have changed the background color when the IsSelected property is “true”. 
 
Code Example[XAML]: 
<listView:SfListView.ItemTemplate> 
  <DataTemplate> 
    <ViewCell> 
      <ViewCell.View> 
        <Grid x:Name="grid" RowSpacing="1"  
              BackgroundColor="{Binding IsSelected, 
                               Converter={StaticResource customconverter}}"> 
           <Entry TextChanged="Entry_TextChanged" VerticalOptions="Center"  
                  HorizontalOptions="Center" HeightRequest="50"/> 
        </Grid> 
      </ViewCell.View> 
    </ViewCell> 
  </DataTemplate> 
</listView:SfListView.ItemTemplate> 
  
Code Example[C#]: 
private void Entry_TextChanged(object sender, TextChangedEventArgs e) 
{ 
  var entry = sender as Entry; 
  var contact = entry.BindingContext as Contacts; 
  if (e.NewTextValue != "") 
     contact.IsSelected = true; 
  else 
     contact.IsSelected = false; 
} 
 
 
public class CustomConverter : IValueConverter 
{ 
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
  { 
    if ((bool)value) 
       return Color.FromHex("#d3d3d3"); 
    return Color.White; 
  } 
} 
  
For your reference, we have attached the sample and you can download it from the below link. 
 
 
Please let us know if you require further assistance. 
 
Regards, 
Dinesh Babu Yadav 


Loader.
Live Chat Icon For mobile
Up arrow icon