Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Trigger dont work with Binding Data

eliseogambaeliseogamba Member ✭✭
edited December 2019 in Xamarin.Forms

Hi, when I update the field to which a binding data trigger is associated, it does not update the "HeightRequest" field.

XML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 x:Class="Go.Views.View">
    <StackLayout>
        <Button Text="Hide Indicator" Clicked="HideIndicator" />
        <ActivityIndicator WidthRequest="40" 
                                   HeightRequest="40" 
                                   Margin="5"  
                                   Color="Black" 
                                   IsRunning="{Binding IsLoading}">
                    <ActivityIndicator.Triggers>
                        <DataTrigger TargetType="ActivityIndicator"  Binding="{Binding IsLoading}" Value="False">
                            <Setter Property="HeightRequest" Value="0" />
                        </DataTrigger>
                    </ActivityIndicator.Triggers>
                </ActivityIndicator>
       </StackLayout>
</ContentPage>

View:

`public partial class View : ContentPage, INotifyPropertyChanged
{
public View()
{
InitializeComponent();

        BindingContext = new MyViewModel();
    }

private void HideIndicator(object sender, EventArgs e)
{
    var ViewModel =BindingContext as MyViewModel;

    ViewModel.IsLoading= false;
}

}`

ViewModel:

`class MyViewModel : INotifyPropertyChanged
{
private bool _IsLoading;

 public bool IsLoading
    {
        get { return _IsLoading; }
        set
        {
            _IsLoading = value;
            RaisePropertyChanged("IsLoading");
        }
    }

public MyViewModel()
{
    IsLoading = true;
}

private void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

}`

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    I created a sample based on the code above. However, it worked on Android, iOS and UWP. What's the Forms version you are developing? Try to update it to the latest and test again.
    Here is the effect on Android:

    I modify the code in the HideIndicator so that the indicator could disappear or appear:

    private void HideIndicator(object sender, EventArgs e)
    {
        var ViewModel = BindingContext as MyViewModel;
    
        ViewModel.IsLoading = !ViewModel.IsLoading;
    }
    
  • eliseogambaeliseogamba Member ✭✭
    > @LandLu said:
    > I created a sample based on the code above. However, it worked on Android, iOS and UWP. What's the Forms version you are developing? Try to update it to the latest and test again.
    > Here is the effect on Android:
    > (Image)
    >
    >
    > I modify the code in the HideIndicator so that the indicator could disappear or appear:
    >
    > private void HideIndicator(object sender, EventArgs e){ var ViewModel = BindingContext as MyViewModel; ViewModel.IsLoading = !ViewModel.IsLoading;}

    The problem is what the Trigger don't set "HeightRequest" in 0, the activity indicator hides correctly.

    If initially, I set Isloading in false, trigger work but if I set IsLoading in false in ViewModel, trigger don't set HeightRequest in 0.

    Any idea? :(
  • LandLuLandLu Member, Xamarin Team Xamurai

    The activity indicator hides because its height has become 0. If its HeightRequest didn't change why it disappeared?

  • eliseogambaeliseogamba Member ✭✭
    For this "IsRunning="{Binding IsLoading}">" (contains en ActivityIndicator in xaml of top)
  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Why not bind IsVisible instead of messing with the HeightRequest?

  • eliseogambaeliseogamba Member ✭✭
    Because ActivityIndicator are in Footer of ListView, and although it is not visible you see a white space
  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited December 2019

    See, there's some necessary context. I don't know if this was ever actually filed as a bug, but in my experience ListView headers and footers do not properly inherit the BindingContext of the ListView. So in order to make bindings work in the footer, you're going to need to also explicitly set the BindingContext of the ActivityIndicator.

  • eliseogambaeliseogamba Member ✭✭
    edited December 2019
    The Binding is of ContentPage, I tried set that Binding in BindingContext of ActivityIndicator and don't work
  • LandLuLandLu Member, Xamarin Team Xamurai

    If we used it directly on a content page as you post above the height will be changed:

    I placed a label beneath the indicator. When the IsLoading changed the position of the label changed too. It proves the height of the indicator does come to 0.
    If you consumed it on the footer of listview it will be different could you please post the code of your usage?

Sign In or Register to comment.