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):

          <Entry x:Name="entry" Text="" Placeholder="required field" />

          <!-- referenced below in DataTrigger-->
          <Button x:Name="button" Text="Save"
              <DataTrigger TargetType="Button"
                            Binding="{Binding Source={x:Reference entry},
                <Setter Property="IsEnabled" Value="False" />

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?


  • 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:

    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=""
                <xamarin:DataTypeTemplateSelector x:Key="DataTypeTemplateSelector">
                        <DataTemplate x:Key="ItemType">
                                    <Frame BackgroundColor="Red" Grid.Column="1" Grid.RowSpan="2">
                                            <DataTrigger TargetType="Frame" Binding="{Binding IsFinished}" Value="True">
                                                <Setter Property="BackgroundColor" Value="Green" />
        <ListView ItemsSource="{Binding Schedule.Items}"
                  ItemTemplate="{StaticResource DataTypeTemplateSelector}" />

    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.