Build error when using DataTrigger inside list item template

SergeyZhizhenkoSergeyZhizhenko RUMember
edited August 2016 in Xamarin.Forms

I need to use DataTrigger in ListView ItemTemplate. I simplified everything till following example (trigger syntax copypasted from Xamarin samples git):

<ListView>
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <ViewCell.View>
          <Entry x:Name="entry" Text="" Placeholder="required field" />

          <!-- referenced below in DataTrigger-->
          <Button x:Name="button" Text="Save"
                  FontSize="Large"
                  HorizontalOptions="Center">
            <Button.Triggers>
              <DataTrigger TargetType="Button"
                            Binding="{Binding Source={x:Reference entry},
                                          Path=Text.Length}"
                            Value="0">
                <Setter Property="IsEnabled" Value="False" />
              </DataTrigger>
            </Button.Triggers>
          </Button>
        </ViewCell.View>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

On build I'm getting error

FeedbackPage.xaml : error : Sequence contains no elements

Googling did not helped :( Can anybody explain is it possible to use triggers in data templates?

Answers

  • JacobPoulsen_JacobPoulsen_ DKMember ✭✭✭

    @SergeyZhizhenko Do you get the error at runtime or during building/compiling?

  • I've mentioned in my question - error occurs on build

  • JacobPoulsen_JacobPoulsen_ DKMember ✭✭✭

    Wops Sorry.

    But well, I see your issue.

    You need to put all your controls inside a Grid or a StackLayout or something, you can't just add them to the ViewCell.View.

  • SergeyZhizhenkoSergeyZhizhenko RUMember
    edited August 2016

    Oh, good catch. During code simplification I've made other error. Really problem is not in layout.
    BUT After some retries and manual projects rebuild error somehow gone :( Magic again...

    Update: I've represented problem again. I have smart DataTemplateSelector - it uses ResourceDictionary as content property to find appropriate DataTemplates by list item type. It is very simple:

    [ContentProperty(nameof(Templates))]
    public class DataTypeTemplateSelector : DataTemplateSelector
    {
        public ResourceDictionary Templates { get; set; }
    
        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            if (item == null)
                throw new Exception("Cannot choose template for NULL item in list");
    
            object template;
            var keyName = item.GetType().FullName;
            if (Templates.TryGetValue(keyName, out template))
            {
                return (DataTemplate)template;
            }
    
            throw new Exception("Unable to get template for item of type " + keyName);
        }
    }
    

    Then I declare this selector in Resources section of ContentPage:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:xamarin="clr-namespace:Quirco.Mobile.Forms.Xamarin;assembly=Quirco.Mobile.Forms.Xamarin"
                 x:Class="Quirco.Mobile.Client.Pages.FeedbackPage">
        <ContentPage.Resources>
            <ResourceDictionary>
                <xamarin:DataTypeTemplateSelector x:Key="DataTypeTemplateSelector">
                    <ResourceDictionary>
                        <DataTemplate x:Key="ItemType">
                            <ViewCell>
                                <ViewCell.View>
                                    <Frame BackgroundColor="Red" Grid.Column="1" Grid.RowSpan="2">
                                        <Frame.Triggers>
                                            <DataTrigger TargetType="Frame" Binding="{Binding IsFinished}" Value="True">
                                                <Setter Property="BackgroundColor" Value="Green" />
                                            </DataTrigger>
                                        </Frame.Triggers>
                                    </Frame>
                                </ViewCell.View>
                            </ViewCell>
                        </DataTemplate>
                    </ResourceDictionary>
                </xamarin:DataTypeTemplateSelector>
    
            </ResourceDictionary>
        </ContentPage.Resources>
    
        <ListView ItemsSource="{Binding Schedule.Items}"
                  HasUnevenRows="True"
                  IsPullToRefreshEnabled="True"
                  ItemTemplate="{StaticResource DataTypeTemplateSelector}" />
    </ContentPage>
    

    And this gives me build error

    FeedbackPage.xaml : error : The given key was not present in the dictionary.
    

    Once I move declaration out of resources and place directly as ListView value - everythin OK. I suspect this is some bug in Xamarin?

  • JosManuelJosManuel USMember ✭✭

    The same is happening to me!

  • Thank you sir, it is the good idea.

  • SteffenMaucySteffenMaucy USMember ✭✭

    2018 and it happend me too. Some bugs live for years in Xamarin.

Sign In or Register to comment.