Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams public string Text { get; set; } public DateTime Date { get; set; } public bool IsActive { get; set; }

This observablecollection is used and binded as ItemSource for ListView. The following is DataTemplate for displaying data -

<DataTemplate>
    <ViewCell>
        <Frame OutlineColor="White" HasShadow="False">
             <!-- Data -->
        </Frame>
    </ViewCell>
</DataTemplate>

Since the ObservableCollection is a collection of Data class which has a boolean property, I want to use it in order to change the frame's background color:

  • If property IsActive is true - BackgroundColor is Red

  • If property IsActive is false - BackgroundColor is Blue

  • I've looked into implementation of Triggers, however I can't seem to get them working correctly, and I'm not sure what I'm missing.

    According to Xamarin Documentation I should be able to do :

    <Frame>
        <Frame.Trigger>
            <!-- -->
        </Frame.Trigger>  
    </Frame>
    

    However that doesn't seem to be possible. Neither is this -

    <Frame>
    <Frame.Style>
        <Style TargetType="Frame">
            <Setter Property="BackgroundColor" Value="Blue" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsActive}" Value="True">
                    <Setter Property="BackgroundColor" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Frame.Style>
    </Frame>
    

    The code above gives the following error message:

    Xamarin.Forms.Xaml.XamlParseException: Position 28:26. The Property TargetType is required to create a Xamarin.Forms.DataTrigger object.
                    Are you sure the line numbers match where your frame style is defined?  I'm not sure about xamarin, but possibly try using the {x:Type } extension instead of relying on string -> type conversion in the TargetType property?
    – user1228
                    Sep 7, 2016 at 17:30
                    Are you setting your BackgroundColor from directly in the <Frame> tag? If so, that property will override any triggered or styled values. Another thing to try is maybe use TargetType={x:Type Frame}, occasionally I see the text string fail to resolve into the actual type, so this prevents that.
    – Rachel
                    Sep 8, 2016 at 20:38
    

    Not sure about your trigger problem but I think you should be able to accomplish the color changing by first implementing INotifyPropertyChanged on your Data class like so:

    public class Data : INotifyPropertyChanged
        public string Text { get; set; }
        public DateTime Date { get; set; }
        private bool _isActive;
        public bool IsActive 
            get { return _isActive; }
                if (value == _isActive)
                    return;
                _isActive = value;
                NotifyPropertyChanged("IsActive");
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    

    Then, in your xaml, you should be able to do something like this:

    <DataTemplate>
        <ViewCell>
            <Frame Background="{Binding IsActive, Converter={StaticResource IsActiveToColorConverter}}" OutlineColor="White" HasShadow="False">
                 <!-- Data -->
            </Frame>
        </ViewCell>
    </DataTemplate>
    

    Where IsActiveToColorConverter looks something like:

    public class IsActiveToColorConverter : IValueConverter
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            var isActive = (bool) value;
            return isActive ? "Red" : "Blue";
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            throw new NotImplementedException();
                    OP Did not mention that boolean property will be changed real time. so NotifyProperty is not required. But Converter is a good option for this scenario. So +1 for that.
    – AVK
                    Sep 7, 2016 at 17:10
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.