Nicoara
Nicoara

Reputation: 398

Binding to a Popup Page

I want to build a popup modal page for my users to select some filters for search but I'm kinda stuck trying to bind the view with the filter. Or I should bind it via properties with the code behind?

This is the view

<?xml version="1.0" encoding="utf-8" ?>
<xct:Popup xmlns="http://xamarin.com/schemas/2014/forms"
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
           xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
           xmlns:local="clr-namespace:Appointments"
           x:TypeArguments="local:Models.Filter"
           Size="400, 400" 
           x:Class="Appointments.Views.Popups.FilterPopup">
    
    <StackLayout>
        <Label Text="{Binding Name}"/>
        <Switch IsToggled="{Binding IsFree}"/>
        <Switch/>
        <Label Text="Minimum rating"/>
        <xct:RangeSlider 
            MaximumValue="5"
            MinimumValue="0"/>

        <Button 
            Text="Apply"
            Clicked="ClosePopup"/>

    </StackLayout>
</xct:Popup>

The code behind

namespace Appointments.Views.Popups
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class FilterPopup : Popup<Filter>
    {
        private Filter filter;
        public string Name { get; set; }
        public FilterPopup(Filter filter)
        {
            filter.IsFreeNow = true;
            filter.Name = "Sergay";
            this.filter = filter;

            InitializeComponent();
            BindingContext = this;
        }

        private void ClosePopup(object sender, EventArgs e)
        {
            filter.IsFreeNow = true;
            filter.IsSchedualeVisible = true;
            Dismiss(filter);
        }
    }
}

And my filter model

namespace Appointments.Models
{
    public class Filter
    {
        public bool IsSchedualeVisible { get; set; }
        public bool IsFreeNow { get; set; }
        public float MinRating { get; set; }

        public string Name { get; set; }
    }
}

Upvotes: 0

Views: 566

Answers (1)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10356

From your code, you set field private Filter filter;, not property public Filter _filter { get; set; }.

As jason said that you need to binding Filter class for FilterPopup's BindingContext directly.

 public partial class FilterPopup 
{   
    public Filter _filter { get; set; }
    public FilterPopup(Filter filter)
    {
        InitializeComponent();
        filter.IsFreeNow = true;
        filter.Name = "Sergay";
        _filter = filter;
        this.BindingContext = _filter ;

    }

    private void ClosePopup(object sender, EventArgs e)
    {
        _filter.IsFreeNow = true;
        _filter.IsSchedualeVisible = true;
        Dismiss(_filter);
    }
}

Upvotes: 1

Related Questions