Using VisualStateManager with ItemTemplateSelector

Hendrik Wittke 6 Reputation points
2020-11-27T15:07:24.213+00:00

For my CollectionViews I often use ItemTemplateSelectors with ItemTemplates that are in different and separate XAML-Files as the actual Page with the CollectionView. But then I can not use the VisualStateManager because the "Selected"-State is not set. If I move my Templates inside my actual Page then it works. I tried already different approaches where to put my style but is has not worked out yet. I that even possible ? I hope I did myself clear !?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,299 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,311 Reputation points Microsoft Vendor
    2020-11-30T02:01:55.633+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I make a test with Entry in the DataTemplate of CollectionView, And I don't set "Selected"-State, I set Normal, Focused and Disabled states. It worked as normal, here is running GIF.

    43395-collectionview.gif

    Here is my layout, I put the VisualStateManager in the Style.

       <ContentPage.Resources>  
       <ResourceDictionary>  
         
                   <Style TargetType="Entry">  
                       <Setter Property="Margin" Value="20, 0" />  
                       <Setter Property="FontSize" Value="18" />  
                       <Setter Property="VisualStateManager.VisualStateGroups">  
                           <VisualStateGroupList>  
                                
                                   <VisualStateGroup x:Name="CommonStates">  
                                       <VisualState x:Name="Normal">  
                                           <VisualState.Setters>  
                                               <Setter Property="BackgroundColor" Value="White" />  
                                           </VisualState.Setters>  
                                       </VisualState>  
         
                                       <VisualState x:Name="Focused">  
                                           <VisualState.Setters>  
                                               <Setter Property="BackgroundColor" Value="Lime" />  
                                           </VisualState.Setters>  
                                       </VisualState>  
         
                                       <VisualState x:Name="Disabled">  
                                           <VisualState.Setters>  
                                               <Setter Property="BackgroundColor" Value="Gray" />  
                                           </VisualState.Setters>  
                                       </VisualState>  
         
                                   </VisualStateGroup>  
                               
                           </VisualStateGroupList>  
                       </Setter>  
                   </Style>  
         
                   <local:MyValueConvert x:Key="intToBool" />  
         
                   <DataTemplate x:Key="validPersonTemplate">  
         
       <Grid>  
       <Grid.ColumnDefinitions>  
       <ColumnDefinition Width="0.3*" />  
       <ColumnDefinition Width="0.2*" />  
       <ColumnDefinition Width="0.2*" />  
                                   <ColumnDefinition Width="0.2*" />  
                           </Grid.ColumnDefinitions>  
       <Label Text="{Binding Name}" TextColor="Green" FontAttributes="Bold" />  
                           
       <Label Grid.Column="1" Text="{Binding DateOfBirth, StringFormat='{0:d}'}" TextColor="Green" />  
                               <Label Grid.Column="2" Text="{Binding Location, Converter={StaticResource intToBool}}" TextColor="Green" HorizontalTextAlignment="End" />  
         
                           <Entry FontSize="18" Grid.Column="3" IsEnabled="{Binding CouldType}" >  
                                 
                           </Entry>  
                       </Grid>  
         
       </DataTemplate>  
       <DataTemplate x:Key="invalidPersonTemplate">  
         
       <Grid>  
       <Grid.ColumnDefinitions>  
                               <ColumnDefinition Width="0.3*" />  
                               <ColumnDefinition Width="0.2*" />  
                               <ColumnDefinition Width="0.2*" />  
                               <ColumnDefinition Width="0.2*" />  
                           </Grid.ColumnDefinitions>  
       <Label Text="{Binding Name}" TextColor="Red" FontAttributes="Bold" />  
       <Label Grid.Column="1" Text="{Binding DateOfBirth, StringFormat='{0:d}'}" TextColor="Red" />  
                               <Label Grid.Column="2" Text="{Binding Location, Converter={StaticResource intToBool}}" TextColor="Red" HorizontalTextAlignment="End" />  
         
                           <Entry FontSize="18" Grid.Column="3" IsEnabled="{Binding CouldType}" >  
                                 
                           </Entry>  
                       </Grid>  
         
       </DataTemplate>  
       <local:PersonDataTemplateSelector x:Key="personDataTemplateSelector" ValidTemplate="{StaticResource validPersonTemplate}" InvalidTemplate="{StaticResource invalidPersonTemplate}" />  
       </ResourceDictionary>  
       </ContentPage.Resources>  
       <StackLayout Margin="20">  
               <CollectionView x:Name="MyCollectionView" ItemsSource="{Binding Monkeys}" ItemTemplate="{StaticResource personDataTemplateSelector}"/>  
       </StackLayout>  
       </ContentPage>  
    

    I tried to put the VisualStateManager in the Entry of DataTemplate directly like following code, it works normally.

       <Entry FontSize="18" Grid.Column="3" IsEnabled="{Binding CouldType}" >  
                               <VisualStateManager.VisualStateGroups>  
                                   <VisualStateGroup x:Name="CommonStates">  
                                       <VisualState x:Name="Normal">  
                                           <VisualState.Setters>  
                                               <Setter Property="BackgroundColor" Value="White" />  
                                           </VisualState.Setters>  
                                       </VisualState>  
         
                                       <VisualState x:Name="Focused">  
                                           <VisualState.Setters>  
                                               <Setter Property="BackgroundColor" Value="Lime" />  
                                           </VisualState.Setters>  
                                       </VisualState>  
         
                                       <VisualState x:Name="Disabled">  
                                           <VisualState.Setters>  
                                               <Setter Property="BackgroundColor" Value="Gray" />  
                                           </VisualState.Setters>  
                                       </VisualState>  
         
                                   </VisualStateGroup>  
                               </VisualStateManager.VisualStateGroups>  
                           </Entry>  
    

    Here is my layout's background code.

       public partial class HomePage : ContentPage  
           {  
               public HomePage()  
               {  
                   InitializeComponent();  
         
                   var people = new List<Person>  
                   {  
                       new Person { Name = "Kath", DateOfBirth = new DateTime(1985, 11, 20), Location = 1  , CouldType=true},  
                       new Person { Name = "Steve", DateOfBirth = new DateTime(1975, 1, 15), Location = 0 , CouldType=false},  
                       new Person { Name = "Lucas", DateOfBirth = new DateTime(1988, 2, 5), Location = 1 , CouldType=true},  
                       new Person { Name = "John", DateOfBirth = new DateTime(1976, 2, 20), Location = 0 , CouldType=false},  
                       new Person { Name = "Tariq", DateOfBirth = new DateTime(1987, 1, 10), Location = 1, CouldType=true },  
                       new Person { Name = "Jane", DateOfBirth = new DateTime(1982, 8, 30), Location = 1 , CouldType=false},  
                       new Person { Name = "Tom", DateOfBirth = new DateTime(1977, 3, 10), Location = 1 , CouldType=true}  
                   };  
         
                   MyCollectionView.ItemsSource = people;  
               }  
           }  
    

    Here is my DataTemplateSelector called PersonDataTemplateSelector.cs.

       public class PersonDataTemplateSelector : DataTemplateSelector  
       {  
       public DataTemplate ValidTemplate { get; set; }  
         
       public DataTemplate InvalidTemplate { get; set; }  
         
       protected override DataTemplate OnSelectTemplate (object item, BindableObject container)  
       {  
       return ((Person)item).DateOfBirth.Year >= 1980 ? ValidTemplate : InvalidTemplate;  
       }  
       }  
    

    Here is valueConvert called MyValueConvert.cs.

       public class MyValueConvert : IValueConverter  
           {  
               public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
               {  
                   return (int)value != 0;  
               }  
         
               public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
               {  
                   return (bool)value ? 1 : 0;  
               }  
           }  
    

    Here is my Person.cs

       public class Person  
           {  
               public string Name { get; set; }  
               public DateTime DateOfBirth { get; set; }  
               public int Location { get; set; }  
         
               public bool CouldType { get; set; }  
           }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments