2

I have ListView using custom template. How do I highlight selected item or tabbed item without using code-behind ?

View

<StackLayout
  Grid.Row="1"
  BackgroundColor="#21576C"
  HorizontalOptions="StartAndExpand"
  VerticalOptions="StartAndExpand">
  <StackLayout
    BackgroundColor="#134359"
    Margin="5,5,5,5">
    <ListView x:Name="Catagoris"
              HorizontalOptions="Start"
              VerticalOptions="FillAndExpand"
              BackgroundColor="Transparent"
              HasUnevenRows="True"
              ItemsSource="{Binding CatagoryType}"
              ItemSelected="ListView_OnItemSelected"
              MinimumHeightRequest="100"
              SeparatorVisibility="None"
        >
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <Label x:Name="ListViewLabel"
                   HorizontalOptions="FillAndExpand"
                   HorizontalTextAlignment="Center"
                   VerticalOptions="FillAndExpand"
                   VerticalTextAlignment="Center"
                   TextColor="#3C9DC5"
                   Text="{Binding ., Converter={StaticResource SpaceOnUpperAndStringToUpperCaseConverter}}"
                   HeightRequest="50">
            </Label>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </StackLayout>
</StackLayout>

Prop in VM

public bool IsCatagorySelected
        {
            get { return _isCatagorySelected; }
            set
            {
                if (_isCatagorySelected != value)
                {
                    OnPropertyChanging(()=>IsCatagorySelected);
                    _isCatagorySelected = value;
                    OnPropertyChanged(() => IsCatagorySelected);
                }
            }
        }

Code behind

private void ListView_OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    MyViewModel myViewModel= sender as  MyViewModel;
    if (e.SelectedItem == null)
    {
        return; 
    }
    myViewModel.IsCatagorySelected= true; 
    DisplayAlert("Item Selected", e.SelectedItem.ToString(), "OK");
}

I need to change the background color and text color of Label 'ListviewLabel'.

5
  • Try in Xamarin.Forms its not that simple. Commented Mar 21, 2017 at 20:08
  • Have you take a look to DataTrigger? blog.xamarin.com/triggers-in-xamarin-forms Commented Mar 21, 2017 at 20:54
  • Thanks for your suggestion Caliaro. Problem here is, I am using DataTemplate type of Label in a ViewCell. Label doesn't have IsSelected property. So trigger is not helpful here to change the BackgroundColor and TextColor of label when an item is selected. Commented Mar 22, 2017 at 15:47
  • Ok, but your listview has a SelectedItem. When you have a SelectedItem you can set a IsSelected property (that you have in your model).. post your model and your xaml Commented Mar 22, 2017 at 15:53
  • @AlessandroCaliaro I have update my post per your request. Commented Mar 22, 2017 at 17:10

1 Answer 1

3

I suggest to take a look to this Blog

you should add to your Model a "Selected" property

public bool Selected { get; set; }

In your ViewModel you should have a property that define the SelectedItem in your ListView

MyModel _selectedItem { get; set; }

and use it in this way

public MyModel SelectedItem
 {
    get { return _selectedItem; }
    set
    {
        if (_selectedItem != null)
            _selectedItem.Selected = false;

        _selectedItem = value;

        if (_selectedItem != null)
            _selectedItem.Selected = true;
    }
 }

then in your XAML you have to bind the ListView's SelectedItem and the TextColor property

    <ListView SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding List}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding Name}" TextColor="{Binding Selected, Converter={StaticResource cnvInvert}}}" FontSize="18"></Label>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>

The TextColor property has an IValueConverter that converter the Selected property to a Color property

public class SelectedToColorConverter : IValueConverter
 {

    #region IValueConverter implementation

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool)
        {
            if ((Boolean)value)
                return Color.Orange;
            else
                return Color.Black;
        }
        return Color.Black;
    }

    public object ConvertBack(object value, Type targetType, object parameter,    System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
 }

in the Blog there is a link to a REPO on GitHub

Sign up to request clarification or add additional context in comments.

4 Comments

is there a way to increase the width of the ListView Separator in PCL level ?
I think you can change it via CustomRenderer. Otherwise you can add a BoxView with a small height and it is your separator
@AlessandroCaliaro Please don't always mix XAML and C#. :-( If you write C# code, everybody can easily translate it to XAML, but the other way around is really difficult.
This is good. But the problem still, is sometimes we need to know the whole context (i.e. the model) in the converter to do a little complex conversion. Like selecting a proper icon based on model name. Now, I am passing the whole view to ConverterParameter and extract its binding context to get that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.