Auto scroll up after dynamically resize view into CollectionView (Xamarin 4.1)

zoyusibuzoyusibu Member ✭✭
edited July 4 in Xamarin.iOS

Hi guys and girls!
In my project it contains CollectionView, which contain viewes with switches. After toggled the switch, each waste item increases in size. Everything is good if the waste is at the top, but i have problems with the bottom of the view: the list (collectionview) automatically scrolls up.
What needs to be done so that the list does not scroll?

Best Answer

Answers

  • AnubhavRanjanAnubhavRanjan INXamarin Team Xamurai

    @zoyusibu It would be great if you could post a screenshot of the same

    .

    .

    .

    .



    Looking for Free* Microsoft Support on Xamarin Issues, visit https://aka.ms/xamarinsupport


  • zoyusibuzoyusibu Member ✭✭
    edited July 5

    @AnubhavRanjan
    please remove space in "http s"
    1st screen - before on click last item
    2nd - after
    I think the problem is that I set the preliminary values for the switch in the method OnBindingContextChanged. But where else can I get the model data?

    http s://us.v-cdn.net/5019960/uploads/editor/xt/03mvoakkh8jp.png
    http s://us.v-cdn.net/5019960/uploads/editor/fr/9l4au55kdz6e.png

  • zoyusibuzoyusibu Member ✭✭
    edited July 5

    My view in collectionview. I don't use XAML

    `public class PaymentBillRelationViewCell: StackLayout
    {
    private readonly XLabel DocumentName;
    private readonly XLabel Date;
    private readonly XLabel Sum;
    private readonly XLabel Remainder;
    private readonly NamedSwitch RelationshipSwitch;
    private readonly NamedInputForForm inputSum;
    private readonly Divider middleDivider;
    private readonly Divider bottomDivider;
    private readonly Action scrollItemToStart;
    private readonly Action<PaymentBillRelationViewData, bool> changeRelationStatus;
    private readonly Action<PaymentBillRelationViewData, decimal?> changeRelationAmount;
    private readonly Func<PaymentBillRelationViewData, bool> isShowRemainder;
    private readonly Func<PaymentBillRelationViewData, decimal> calculateRelationRemainder;
    private readonly Func<PaymentBillRelationViewData, string> checkRelationForError;
    private readonly Action updateHeader;
    private readonly Grid grid;
    public PaymentBillRelationViewCell(
    Action scrollItemToStart,
    Action<PaymentBillRelationViewData, bool> changeRelationStatus,
    Action<PaymentBillRelationViewData, decimal?> changeRelationAmount,
    Func<PaymentBillRelationViewData, bool> isShowRemainder,
    Func<PaymentBillRelationViewData, decimal> calculateRelationRemainder,
    Func<PaymentBillRelationViewData, string> checkRelationForError,
    Action updateHeader,
    bool hasBottomButton = false,
    string textForButton = "",
    Action goToCreateMoney = null)
    {
    this.scrollItemToStart = scrollItemToStart;
    this.changeRelationStatus = changeRelationStatus;
    this.changeRelationAmount = changeRelationAmount;
    this.isShowRemainder = isShowRemainder;
    this.calculateRelationRemainder = calculateRelationRemainder;
    this.checkRelationForError = checkRelationForError;
    this.updateHeader = updateHeader;
    grid = new Grid
    {
    ColumnDefinitions = new ColumnDefinitionCollection
    {
    new ColumnDefinition
    {
    Width = GridLength.Auto
    },
    new ColumnDefinition
    {
    Width = GridLength.Auto
    },
    new ColumnDefinition
    {
    Width = GridLength.Star
    },
    new ColumnDefinition
    {
    Width = Sizes.FormItemRightPadding
    }
    },
    RowDefinitions = new RowDefinitionCollection
    {
    new RowDefinition { Height = GridLength.Auto },
    new RowDefinition { Height = GridLength.Auto },
    new RowDefinition { Height = GridLength.Auto },
    new RowDefinition { Height = GridLength.Auto },
    new RowDefinition { Height = GridLength.Auto },
    new RowDefinition { Height = GridLength.Auto },
    new RowDefinition { Height = GridLength.Auto },
    new RowDefinition { Height = GridLength.Auto }
    }
    };

            grid.Children.Add(GetTopBoldDivider().Export(out var boldDividerLayout), 0, 0);
            Grid.SetColumnSpan(boldDividerLayout, 4);
            grid.Children.Add(new XLabel
                              {
                                  FontSize = FontSizes.Standard17,
                                  VerticalOptions = LayoutOptions.CenterAndExpand,
                                  HorizontalOptions = LayoutOptions.Start,
                                  VerticalTextAlignment = TextAlignment.Center,
                                  Margin = new Thickness
                                           {
                                               Left = Sizes.FormItemLeftPadding
                                           }
                              }.Export(out DocumentName), 0, 1);
            Grid.SetColumnSpan(DocumentName, 3);
            grid.Children.Add(new XLabel
                              {
                                  FontSize = FontSizes.Standard17,
                                  VerticalOptions = LayoutOptions.CenterAndExpand,
                                  VerticalTextAlignment = TextAlignment.Center,
                                  Margin = new Thickness
                                           {
                                               Left = Sizes.FormItemLeftPadding
                                           }
                              }.Export(out Date), 0, 2);
            grid.Children.Add(new XLabel
                              {
                                  FontSize = FontSizes.Standard17,
                                  VerticalOptions = LayoutOptions.CenterAndExpand,
                                  VerticalTextAlignment = TextAlignment.Center,
                                  Margin = new Thickness
                                           {
                                               Left = Sizes.FormItemLeftPadding
                                           }
                              }.Export(out Sum), 0, 3);
            Grid.SetColumnSpan(Sum, 3);
            grid.Children.Add(new NamedSwitch("").Export(out RelationshipSwitch), 2, 1);
            grid.Children.Add(new XLabel
                              {
                                  FontSize = FontSizes.DashboardCellText,
                                  VerticalOptions = LayoutOptions.End,
                                  HorizontalOptions = LayoutOptions.StartAndExpand,
                                  VerticalTextAlignment = TextAlignment.Center,
                                  TextColor = Colors.TextDisableColor,
                                  Margin = new Thickness
                                           {
                                               Left = Sizes.FormItemLeftPadding
                                           },
                                  IsVisible = false
                              }.Export(out Remainder), 0, 4);
            Grid.SetRowSpan(RelationshipSwitch, 4);
            Grid.SetColumnSpan(Remainder, 4);
            grid.Children.Add(new Divider().Export(out middleDivider), 0, 5);
            Grid.SetColumnSpan(middleDivider, 4);
            grid.Children.Add(new NamedInputForForm(new NamedInput("Связано, ₽", true, maxLength: 30)
                                                    {
                                                        Keyboard = Keyboard.Numeric,
                                                        Padding = new Thickness { Bottom = 8 },
                                                    }).Export(out inputSum), 0, 6);
            Grid.SetColumnSpan(inputSum, 4);
            grid.Children.Add(new Divider
                              {
                                  VerticalOptions = LayoutOptions.EndAndExpand,
                                  FullWidth = true
                              }.Export(out bottomDivider), 0, 7);
            Grid.SetColumnSpan(bottomDivider, 4);
            RelationshipSwitch.SwitchControl.Toggled += (_, __) => ChangeRelation();
            inputSum.Content.TextChanged += (_, __) => ChangeRelatedSum();
            inputSum.DividerVisible = false;
            grid.BackgroundColor = Color.White;
    
            Children.Add(grid);
            if (hasBottomButton)
                Children.Add(new PaymentBillRelationFooter(textForButton, goToCreateMoney));
        }
    
        private LightStackLayout GetTopBoldDivider()
        {
            return new LightStackLayout
                   {
                       Children =
                       {
                           new Divider(Colors.LightGrayDivider)
                           {
                               VerticalOptions = LayoutOptions.End,
                               FullWidth = true,
                               HeightRequest = 16,
                           },
                           new Divider
                           {
                               VerticalOptions = LayoutOptions.End,
                               FullWidth = true
                           }
                       }
                   };
        }
    
        private void SetItem(PaymentBillRelationViewData data)
        {
            if (inputSum.IsVisible == RelationshipSwitch.Value)
                return;
            var item = (PaymentBillRelationViewData) BindingContext;
            RelationshipSwitch.Value = item.ConnectedSumToCurrentDoc > 0;
            inputSum.Text = item.ConnectedSumToCurrentDoc != 0
                                ? item.ConnectedSumToCurrentDoc.ToString()
                                : "";
            if (isShowRemainder.Invoke(item))
            {
                Remainder.IsVisible = true;
                Remainder.Text =
                    $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
                middleDivider.SetValue(Grid.RowProperty, 5);
                inputSum.SetValue(Grid.RowProperty, 6);
                RelationshipSwitch.SetValue(Grid.RowSpanProperty, 4);
            }
            else
            {
                Remainder.IsVisible = false;
                middleDivider.SetValue(Grid.RowProperty, 6);
                inputSum.SetValue(Grid.RowProperty, 7);
                RelationshipSwitch.SetValue(Grid.RowSpanProperty, 3);
            }
    
            if (RelationshipSwitch.Value)
            {
                inputSum.Text = item.ConnectedSumToCurrentDoc.ToString();
            }
    
            inputSum.Focused += () => scrollItemToStart.Invoke(item);
            RelationshipSwitch.Value = item.ConnectedSumToCurrentDoc > 0;
            inputSum.IsVisible = RelationshipSwitch.Value;
            middleDivider.IsVisible = RelationshipSwitch.Value;
            DocumentName.Text = item.DocumentName;
            Date.Text = item.DateOfDocument;
            Sum.Text = $"{item.AllAmountMoney.FormatMoneyWithCoopeks()} {MoneyHelpers.RubleSumbol}";
        }
    
        private void ChangeRelatedSum()
        {
            var item = (PaymentBillRelationViewData) BindingContext;
            if (item == null)
                return;
            decimal? inputSum = null;
            try
            {
                if (this.inputSum.Text.Length > 0)
                    inputSum = decimal.Parse(this.inputSum.Text);
            }
            catch (Exception e)
            {
            }
    
            changeRelationAmount?.Invoke(item, inputSum);
            if (isShowRemainder.Invoke(item))
                Remainder.Text =
                    $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
            var textError = checkRelationForError?.Invoke(item);
            if (textError.Length > 0)
            {
                this.inputSum.Content.Hint = textError;
                this.inputSum.Content.HintColor = Color.Red;
                bottomDivider.BackgroundColor = Color.Red;
            }
            else
            {
                this.inputSum.Content.Hint = "Связано, ₽";
                this.inputSum.Content.HintColor = Colors.TextDisableColor;
                bottomDivider.BackgroundColor = Colors.Divider;
            }
    
            updateHeader?.Invoke();
        }
    
    
        private void ChangeRelation()
        {
            var item = (PaymentBillRelationViewData) BindingContext;
            if (inputSum.IsVisible == RelationshipSwitch.Value)
                return;
            inputSum.IsVisible = RelationshipSwitch.Value;
            middleDivider.IsVisible = RelationshipSwitch.Value;
            changeRelationStatus?.Invoke(item, RelationshipSwitch.Value);
            inputSum.Text = RelationshipSwitch.Value ? item.ConnectedSumToCurrentDoc.ToString() : "";
            if (isShowRemainder.Invoke(item))
                Remainder.Text =
                    $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
            updateHeader?.Invoke();
        }
    
        protected override void OnBindingContextChanged()
        {
            if (inputSum.IsVisible == RelationshipSwitch.Value)
                return;
            base.OnBindingContextChanged();
            var data = (PaymentBillRelationViewData) BindingContext;
            if (data == null)
                return;
    
            DocumentName.Text = data.DocumentName;
            Date.Text = data.DateOfDocument;
            Sum.Text = $"{data.AllAmountMoney.FormatMoneyWithCoopeks()} {MoneyHelpers.RubleSumbol}";
            SetItem(data);
        }
    }`
    
  • zoyusibuzoyusibu Member ✭✭
    edited July 5

    My view in collectionView. I don't use XAML

        namespace Mobile.iOS.Screens.KdrLines.PaymentBillRelations
        {
            public class PaymentBillRelationViewCell: StackLayout
            {
                private readonly XLabel DocumentName;
                private readonly XLabel Date;
                private readonly XLabel Sum;
                private readonly XLabel Remainder;
                private readonly NamedSwitch RelationshipSwitch;
                private readonly NamedInputForForm inputSum;
                private readonly Divider middleDivider;
                private readonly Divider bottomDivider;
                private readonly Action<PaymentBillRelationViewData> scrollItemToStart;
                private readonly Action<PaymentBillRelationViewData, bool> changeRelationStatus;
                private readonly Action<PaymentBillRelationViewData, decimal?> changeRelationAmount;
                private readonly Func<PaymentBillRelationViewData, bool> isShowRemainder;
                private readonly Func<PaymentBillRelationViewData, decimal> calculateRelationRemainder;
                private readonly Func<PaymentBillRelationViewData, string> checkRelationForError;
                private readonly Action updateHeader;
    
                private readonly Grid grid;
    
                public PaymentBillRelationViewCell(
                    Action<PaymentBillRelationViewData> scrollItemToStart,
                    Action<PaymentBillRelationViewData, bool> changeRelationStatus,
                    Action<PaymentBillRelationViewData, decimal?> changeRelationAmount,
                    Func<PaymentBillRelationViewData, bool> isShowRemainder,
                    Func<PaymentBillRelationViewData, decimal> calculateRelationRemainder,
                    Func<PaymentBillRelationViewData, string> checkRelationForError,
                    Action updateHeader,
                    bool hasBottomButton = false,
                    string textForButton = "",
                    Action goToCreateMoney = null)
                {
                    this.scrollItemToStart = scrollItemToStart;
                    this.changeRelationStatus = changeRelationStatus;
                    this.changeRelationAmount = changeRelationAmount;
                    this.isShowRemainder = isShowRemainder;
                    this.calculateRelationRemainder = calculateRelationRemainder;
                    this.checkRelationForError = checkRelationForError;
                    this.updateHeader = updateHeader;
                    grid = new Grid
                           {
                               ColumnDefinitions = new ColumnDefinitionCollection
                                                   {
                                                       new ColumnDefinition
                                                       {
                                                           Width = GridLength.Auto
                                                       },
                                                       new ColumnDefinition
                                                       {
                                                           Width = GridLength.Auto
                                                       },
                                                       new ColumnDefinition
                                                       {
                                                           Width = GridLength.Star
                                                       },
                                                       new ColumnDefinition
                                                       {
                                                           Width = Sizes.FormItemRightPadding
                                                       }
                                                   },
                               RowDefinitions = new RowDefinitionCollection
                                                {
                                                    new RowDefinition { Height = GridLength.Auto },
                                                    new RowDefinition { Height = GridLength.Auto },
                                                    new RowDefinition { Height = GridLength.Auto },
                                                    new RowDefinition { Height = GridLength.Auto },
                                                    new RowDefinition { Height = GridLength.Auto },
                                                    new RowDefinition { Height = GridLength.Auto },
                                                    new RowDefinition { Height = GridLength.Auto },
                                                    new RowDefinition { Height = GridLength.Auto }
                                                }
                           };
    
                    grid.Children.Add(GetTopBoldDivider().Export(out var boldDividerLayout), 0, 0);
                    Grid.SetColumnSpan(boldDividerLayout, 4);
                    grid.Children.Add(new XLabel
                                      {
                                          FontSize = FontSizes.Standard17,
                                          VerticalOptions = LayoutOptions.CenterAndExpand,
                                          HorizontalOptions = LayoutOptions.Start,
                                          VerticalTextAlignment = TextAlignment.Center,
                                          Margin = new Thickness
                                                   {
                                                       Left = Sizes.FormItemLeftPadding
                                                   }
                                      }.Export(out DocumentName), 0, 1);
                    Grid.SetColumnSpan(DocumentName, 3);
                    grid.Children.Add(new XLabel
                                      {
                                          FontSize = FontSizes.Standard17,
                                          VerticalOptions = LayoutOptions.CenterAndExpand,
                                          VerticalTextAlignment = TextAlignment.Center,
                                          Margin = new Thickness
                                                   {
                                                       Left = Sizes.FormItemLeftPadding
                                                   }
                                      }.Export(out Date), 0, 2);
                    grid.Children.Add(new XLabel
                                      {
                                          FontSize = FontSizes.Standard17,
                                          VerticalOptions = LayoutOptions.CenterAndExpand,
                                          VerticalTextAlignment = TextAlignment.Center,
                                          Margin = new Thickness
                                                   {
                                                       Left = Sizes.FormItemLeftPadding
                                                   }
                                      }.Export(out Sum), 0, 3);
                    Grid.SetColumnSpan(Sum, 3);
                    grid.Children.Add(new NamedSwitch("").Export(out RelationshipSwitch), 2, 1);
                    grid.Children.Add(new XLabel
                                      {
                                          FontSize = FontSizes.DashboardCellText,
                                          VerticalOptions = LayoutOptions.End,
                                          HorizontalOptions = LayoutOptions.StartAndExpand,
                                          VerticalTextAlignment = TextAlignment.Center,
                                          TextColor = Colors.TextDisableColor,
                                          Margin = new Thickness
                                                   {
                                                       Left = Sizes.FormItemLeftPadding
                                                   },
                                          IsVisible = false
                                      }.Export(out Remainder), 0, 4);
                    Grid.SetRowSpan(RelationshipSwitch, 4);
                    Grid.SetColumnSpan(Remainder, 4);
                    grid.Children.Add(new Divider().Export(out middleDivider), 0, 5);
                    Grid.SetColumnSpan(middleDivider, 4);
                    grid.Children.Add(new NamedInputForForm(new NamedInput("Связано, ₽", true, maxLength: 30)
                                                            {
                                                                Keyboard = Keyboard.Numeric,
                                                                Padding = new Thickness { Bottom = 8 },
                                                            }).Export(out inputSum), 0, 6);
                    Grid.SetColumnSpan(inputSum, 4);
                    grid.Children.Add(new Divider
                                      {
                                          VerticalOptions = LayoutOptions.EndAndExpand,
                                          FullWidth = true
                                      }.Export(out bottomDivider), 0, 7);
                    Grid.SetColumnSpan(bottomDivider, 4);
                    RelationshipSwitch.SwitchControl.Toggled += (_, __) => ChangeRelation();
                    inputSum.Content.TextChanged += (_, __) => ChangeRelatedSum();
                    inputSum.DividerVisible = false;
                    grid.BackgroundColor = Color.White;
                    Children.Add(grid);
                    if (hasBottomButton)
                        Children.Add(new PaymentBillRelationFooter(textForButton, goToCreateMoney));
                }
    
                private LightStackLayout GetTopBoldDivider()
                {
                    return new LightStackLayout
                           {
                               Children =
                               {
                                   new Divider(Colors.LightGrayDivider)
                                   {
                                       VerticalOptions = LayoutOptions.End,
                                       FullWidth = true,
                                       HeightRequest = 16,
                                   },
                                   new Divider
                                   {
                                       VerticalOptions = LayoutOptions.End,
                                       FullWidth = true
                                   }
                               }
                           };
                }
    
                protected override void OnBindingContextChanged()
                {
    
                    var data = (PaymentBillRelationViewData) BindingContext;
                    if (data == null)
                        return;
    
                    SetData(data);
                    base.OnBindingContextChanged();
    
                }
    
                private void SetData(PaymentBillRelationViewData item)
            {
                if (inputSum.IsVisible == RelationshipSwitch.Value)
                    return;
    
                if (isShowRemainder.Invoke(item))
                {
                    Remainder.IsVisible = true;
                    Remainder.Text =
                    $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
                    middleDivider.SetValue(Grid.RowProperty, 5);
                    inputSum.SetValue(Grid.RowProperty, 6);
                RelationshipSwitch.SetValue(Grid.RowSpanProperty, 4);
                }
                else
                {
                    Remainder.IsVisible = false;
                    middleDivider.SetValue(Grid.RowProperty, 6);
                    inputSum.SetValue(Grid.RowProperty, 7);
                    RelationshipSwitch.SetValue(Grid.RowSpanProperty, 3);
                }
    
                inputSum.Focused += () => scrollItemToStart.Invoke(item);
                RelationshipSwitch.Value = item.ConnectedSumToCurrentDoc > 0;
                inputSum.IsVisible = RelationshipSwitch.Value;
                middleDivider.IsVisible = RelationshipSwitch.Value;
                if (inputSum.IsVisible)
                    inputSum.Text = item.ConnectedSumToCurrentDoc != 0
                                    ? item.ConnectedSumToCurrentDoc.ToString()
                                    : "";
    
                DocumentName.Text = item.DocumentName;
                Date.Text = item.DateOfDocument;
                Sum.Text = $"{item.AllAmountMoney.FormatMoneyWithCoopeks()} {MoneyHelpers.RubleSumbol}";
            }
    
                private void ChangeRelatedSum()
                {
                    var item = (PaymentBillRelationViewData) BindingContext;
                    if (item == null)
                        return;
                    decimal? inputSum = null;
                    try
                    {
                        if (this.inputSum.Text.Length > 0)
                            inputSum = decimal.Parse(this.inputSum.Text);
                    }
                    catch (Exception e)
                    {
                    }
    
                    changeRelationAmount?.Invoke(item, inputSum);
                    if (isShowRemainder.Invoke(item))
                        Remainder.Text =
                            $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
                    var textError = checkRelationForError?.Invoke(item);
                    if (textError.Length > 0)
                    {
                        this.inputSum.Content.Hint = textError;
                        this.inputSum.Content.HintColor = Color.Red;
                        bottomDivider.BackgroundColor = Color.Red;
                    }
                    else
                    {
                        this.inputSum.Content.Hint = "Связано, ₽";
                        this.inputSum.Content.HintColor = Colors.TextDisableColor;
                        bottomDivider.BackgroundColor = Colors.Divider;
                    }
    
                    updateHeader?.Invoke();
                }
    
                private void ChangeRelation()
                {
                    var item = (PaymentBillRelationViewData) BindingContext;
                    if (inputSum.IsVisible == RelationshipSwitch.Value)
                        return;
                    inputSum.IsVisible = RelationshipSwitch.Value;
                    middleDivider.IsVisible = RelationshipSwitch.Value;
                    changeRelationStatus?.Invoke(item, RelationshipSwitch.Value);
                    inputSum.Text = RelationshipSwitch.Value ? item.ConnectedSumToCurrentDoc.ToString() : "";
                    if (isShowRemainder.Invoke(item))
                        Remainder.Text =
                            $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
                    updateHeader?.Invoke();
                }
            }
        }
    
  • zoyusibuzoyusibu Member ✭✭

    Code in view of CollectionView. I don't use XAML

    public class PaymentBillRelationViewCell: StackLayout
        {
            private readonly XLabel DocumentName;
            private readonly XLabel Date;
            private readonly XLabel Sum;
            private readonly XLabel Remainder;
            private readonly NamedSwitch RelationshipSwitch;
            private readonly NamedInputForForm inputSum;
            private readonly Divider middleDivider;
            private readonly Divider bottomDivider;
            private readonly Action<PaymentBillRelationViewData> scrollItemToStart;
            private readonly Action<PaymentBillRelationViewData, bool> changeRelationStatus;
            private readonly Action<PaymentBillRelationViewData, decimal?> changeRelationAmount;
            private readonly Func<PaymentBillRelationViewData, bool> isShowRemainder;
            private readonly Func<PaymentBillRelationViewData, decimal> calculateRelationRemainder;
            private readonly Func<PaymentBillRelationViewData, string> checkRelationForError;
            private readonly Action updateHeader;
    
            private readonly Grid grid;
    
            public PaymentBillRelationViewCell(
                Action<PaymentBillRelationViewData> scrollItemToStart,
                Action<PaymentBillRelationViewData, bool> changeRelationStatus,
                Action<PaymentBillRelationViewData, decimal?> changeRelationAmount,
                Func<PaymentBillRelationViewData, bool> isShowRemainder,
                Func<PaymentBillRelationViewData, decimal> calculateRelationRemainder,
                Func<PaymentBillRelationViewData, string> checkRelationForError,
                Action updateHeader,
                bool hasBottomButton = false,
                string textForButton = "",
                Action goToCreateMoney = null)
            {
                this.scrollItemToStart = scrollItemToStart;
                this.changeRelationStatus = changeRelationStatus;
                this.changeRelationAmount = changeRelationAmount;
                this.isShowRemainder = isShowRemainder;
                this.calculateRelationRemainder = calculateRelationRemainder;
                this.checkRelationForError = checkRelationForError;
                this.updateHeader = updateHeader;
                grid = new Grid
                       {
                           ColumnDefinitions = new ColumnDefinitionCollection
                                               {
                                                   new ColumnDefinition
                                                   {
                                                       Width = GridLength.Auto
                                                   },
                                                   new ColumnDefinition
                                                   {
                                                       Width = GridLength.Auto
                                                   },
                                                   new ColumnDefinition
                                                   {
                                                       Width = GridLength.Star
                                                   },
                                                   new ColumnDefinition
                                                   {
                                                       Width = Sizes.FormItemRightPadding
                                                   }
                                               },
                           RowDefinitions = new RowDefinitionCollection
                                            {
                                                new RowDefinition { Height = GridLength.Auto },
                                                new RowDefinition { Height = GridLength.Auto },
                                                new RowDefinition { Height = GridLength.Auto },
                                                new RowDefinition { Height = GridLength.Auto },
                                                new RowDefinition { Height = GridLength.Auto },
                                                new RowDefinition { Height = GridLength.Auto },
                                                new RowDefinition { Height = GridLength.Auto },
                                                new RowDefinition { Height = GridLength.Auto }
                                            }
                       };
    
                grid.Children.Add(GetTopBoldDivider().Export(out var boldDividerLayout), 0, 0);
                Grid.SetColumnSpan(boldDividerLayout, 4);
                grid.Children.Add(new XLabel
                                  {
                                      FontSize = FontSizes.Standard17,
                                      VerticalOptions = LayoutOptions.CenterAndExpand,
                                      HorizontalOptions = LayoutOptions.Start,
                                      VerticalTextAlignment = TextAlignment.Center,
                                      Margin = new Thickness
                                               {
                                                   Left = Sizes.FormItemLeftPadding
                                               }
                                  }.Export(out DocumentName), 0, 1);
                Grid.SetColumnSpan(DocumentName, 3);
                grid.Children.Add(new XLabel
                                  {
                                      FontSize = FontSizes.Standard17,
                                      VerticalOptions = LayoutOptions.CenterAndExpand,
                                      VerticalTextAlignment = TextAlignment.Center,
                                      Margin = new Thickness
                                               {
                                                   Left = Sizes.FormItemLeftPadding
                                               }
                                  }.Export(out Date), 0, 2);
                grid.Children.Add(new XLabel
                                  {
                                      FontSize = FontSizes.Standard17,
                                      VerticalOptions = LayoutOptions.CenterAndExpand,
                                      VerticalTextAlignment = TextAlignment.Center,
                                      Margin = new Thickness
                                               {
                                                   Left = Sizes.FormItemLeftPadding
                                               }
                                  }.Export(out Sum), 0, 3);
                Grid.SetColumnSpan(Sum, 3);
                grid.Children.Add(new NamedSwitch("").Export(out RelationshipSwitch), 2, 1);
                grid.Children.Add(new XLabel
                                  {
                                      FontSize = FontSizes.DashboardCellText,
                                      VerticalOptions = LayoutOptions.End,
                                      HorizontalOptions = LayoutOptions.StartAndExpand,
                                      VerticalTextAlignment = TextAlignment.Center,
                                      TextColor = Colors.TextDisableColor,
                                      Margin = new Thickness
                                               {
                                                   Left = Sizes.FormItemLeftPadding
                                               },
                                      IsVisible = false
                                  }.Export(out Remainder), 0, 4);
                Grid.SetRowSpan(RelationshipSwitch, 4);
                Grid.SetColumnSpan(Remainder, 4);
                grid.Children.Add(new Divider().Export(out middleDivider), 0, 5);
                Grid.SetColumnSpan(middleDivider, 4);
                grid.Children.Add(new NamedInputForForm(new NamedInput("Связано, ₽", true, maxLength: 30)
                                                        {
                                                            Keyboard = Keyboard.Numeric,
                                                            Padding = new Thickness { Bottom = 8 },
                                                        }).Export(out inputSum), 0, 6);
                Grid.SetColumnSpan(inputSum, 4);
                grid.Children.Add(new Divider
                                  {
                                      VerticalOptions = LayoutOptions.EndAndExpand,
                                      FullWidth = true
                                  }.Export(out bottomDivider), 0, 7);
                Grid.SetColumnSpan(bottomDivider, 4);
                RelationshipSwitch.SwitchControl.Toggled += (_, __) => ChangeRelation();
                inputSum.Content.TextChanged += (_, __) => ChangeRelatedSum();
                inputSum.DividerVisible = false;
                grid.BackgroundColor = Color.White;
                Children.Add(grid);
                if (hasBottomButton)
                    Children.Add(new PaymentBillRelationFooter(textForButton, goToCreateMoney));
            }
    
            private LightStackLayout GetTopBoldDivider()
            {
                return new LightStackLayout
                       {
                           Children =
                           {
                               new Divider(Colors.LightGrayDivider)
                               {
                                   VerticalOptions = LayoutOptions.End,
                                   FullWidth = true,
                                   HeightRequest = 16,
                               },
                               new Divider
                               {
                                   VerticalOptions = LayoutOptions.End,
                                   FullWidth = true
                               }
                           }
                       };
            }
    
            protected override void OnBindingContextChanged()
            {
                var data = (PaymentBillRelationViewData) BindingContext;
                if (data == null)
                    return;
    
                SetData(data);
                base.OnBindingContextChanged();
            }
    
            private void SetData(PaymentBillRelationViewData item)
            {
                if (inputSum.IsVisible == RelationshipSwitch.Value)
                    return;
    
                if (isShowRemainder.Invoke(item))
                {
                    Remainder.IsVisible = true;
                    Remainder.Text =
                        $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
                    middleDivider.SetValue(Grid.RowProperty, 5);
                    inputSum.SetValue(Grid.RowProperty, 6);
                    RelationshipSwitch.SetValue(Grid.RowSpanProperty, 4);
                }
                else
                {
                    Remainder.IsVisible = false;
                    middleDivider.SetValue(Grid.RowProperty, 6);
                    inputSum.SetValue(Grid.RowProperty, 7);
                    RelationshipSwitch.SetValue(Grid.RowSpanProperty, 3);
                }
    
                inputSum.Focused += () => scrollItemToStart.Invoke(item);
                RelationshipSwitch.Value = item.ConnectedSumToCurrentDoc > 0;
                inputSum.IsVisible = RelationshipSwitch.Value;
                middleDivider.IsVisible = RelationshipSwitch.Value;
                if (inputSum.IsVisible)
                    inputSum.Text = item.ConnectedSumToCurrentDoc != 0
                                        ? item.ConnectedSumToCurrentDoc.ToString()
                                        : "";
    
                DocumentName.Text = item.DocumentName;
                Date.Text = item.DateOfDocument;
                Sum.Text = $"{item.AllAmountMoney.FormatMoneyWithCoopeks()} {MoneyHelpers.RubleSumbol}";
            }
    
            private void ChangeRelatedSum()
            {
                var item = (PaymentBillRelationViewData) BindingContext;
                if (item == null)
                    return;
                decimal? inputSum = null;
                try
                {
                    if (this.inputSum.Text.Length > 0)
                        inputSum = decimal.Parse(this.inputSum.Text);
                }
                catch (Exception e)
                {
                }
    
                changeRelationAmount?.Invoke(item, inputSum);
                if (isShowRemainder.Invoke(item))
                    Remainder.Text =
                        $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
                var textError = checkRelationForError?.Invoke(item);
                if (textError.Length > 0)
                {
                    this.inputSum.Content.Hint = textError;
                    this.inputSum.Content.HintColor = Color.Red;
                    bottomDivider.BackgroundColor = Color.Red;
                }
                else
                {
                    this.inputSum.Content.Hint = "Связано, ₽";
                    this.inputSum.Content.HintColor = Colors.TextDisableColor;
                    bottomDivider.BackgroundColor = Colors.Divider;
                }
    
                updateHeader?.Invoke();
            }
    
            private void ChangeRelation()
            {
                var item = (PaymentBillRelationViewData) BindingContext;
                if (inputSum.IsVisible == RelationshipSwitch.Value)
                    return;
                inputSum.IsVisible = RelationshipSwitch.Value;
                middleDivider.IsVisible = RelationshipSwitch.Value;
                changeRelationStatus?.Invoke(item, RelationshipSwitch.Value);
                inputSum.Text = RelationshipSwitch.Value ? item.ConnectedSumToCurrentDoc.ToString() : "";
                if (isShowRemainder.Invoke(item))
                    Remainder.Text =
                        $"Осталось связать: {calculateRelationRemainder?.Invoke(item)} {MoneyHelpers.RubleSumbol}";
                updateHeader?.Invoke();
            }
        }
    
  • zoyusibuzoyusibu Member ✭✭
    edited July 5

    the page jumps even when the cell size does not change
    Please, help me anybody :(

Sign In or Register to comment.