Xamarin Forms Custom Stepper with 2 buttons and Entry











up vote
1
down vote

favorite
2












I implemented this CustomStepper:



 using System;
using Xamarin.Forms;

namespace AppXamarin
{
public class CustomStepper : StackLayout
{

Button PlusBtn;
Button MinusBtn;
Entry Entry;

public static readonly BindableProperty TextProperty =
BindableProperty.Create(
propertyName: "Text",
returnType: typeof(int),
declaringType: typeof(CustomStepper),
defaultValue: 0,
defaultBindingMode: BindingMode.TwoWay);

public int Text
{
get { return (int)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public CustomStepper()
{
PlusBtn = new Button { WidthRequest = 30, HeightRequest = 30 };
MinusBtn = new Button { WidthRequest = 30, HeightRequest = 30 };
PlusBtn.Image = "exp20181029Artboard51";
MinusBtn.Image = "exp20181029Artboard52";
switch (Device.RuntimePlatform)
{
case Device.UWP:
case Device.Android:
{
PlusBtn.BackgroundColor = Color.Transparent;
MinusBtn.BackgroundColor = Color.Transparent;
break;
}
case Device.iOS:
{
PlusBtn.BackgroundColor = Color.Transparent;
MinusBtn.BackgroundColor = Color.Transparent;
break;
}
}
Orientation = StackOrientation.Horizontal;
PlusBtn.Clicked += PlusBtn_Clicked;
MinusBtn.Clicked += MinusBtn_Clicked;
Entry = new Entry { PlaceholderColor = Color.Gray, Keyboard = Keyboard.Numeric, WidthRequest = 30, BackgroundColor = Color.Transparent, FontSize = 15 };
Entry.Keyboard = Keyboard.Numeric;
Entry.Behaviors.Add(new NumericValidationBehavior());
Entry.SetBinding(Entry.TextProperty, new Binding(nameof(Text), BindingMode.TwoWay, source: this));
Entry.HorizontalTextAlignment = TextAlignment.Center;
Entry.TextChanged += Entry_TextChanged;
Children.Add(MinusBtn);
Children.Add(Entry);
Children.Add(PlusBtn);
}
private void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(e.NewTextValue) && e.NewTextValue != ".")
this.Text = int.Parse(e.NewTextValue);
}

private void MinusBtn_Clicked(object sender, EventArgs e)
{
if (Text > 0)
Text--;
}

private void PlusBtn_Clicked(object sender, EventArgs e)
{
Text++;
}

}
}


When placing normally in the page I can access it and take the text property and use it in my Xaml.cs code. But in my case, I'm placing it inside a listview and as you know in listview the items are bindable I can't access it directly. In the regular stepper when it is placed in the listview we can use the "ValueChanged" method and can easily get the value by using e.NewValue in the "ValueChanged" method in the Xaml.cs file. Is there a way that I can add something to the CustomStepper class that can help me access the Text property and uses it in the Xaml.cs file? Thanks in advance










share|improve this question




























    up vote
    1
    down vote

    favorite
    2












    I implemented this CustomStepper:



     using System;
    using Xamarin.Forms;

    namespace AppXamarin
    {
    public class CustomStepper : StackLayout
    {

    Button PlusBtn;
    Button MinusBtn;
    Entry Entry;

    public static readonly BindableProperty TextProperty =
    BindableProperty.Create(
    propertyName: "Text",
    returnType: typeof(int),
    declaringType: typeof(CustomStepper),
    defaultValue: 0,
    defaultBindingMode: BindingMode.TwoWay);

    public int Text
    {
    get { return (int)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
    }
    public CustomStepper()
    {
    PlusBtn = new Button { WidthRequest = 30, HeightRequest = 30 };
    MinusBtn = new Button { WidthRequest = 30, HeightRequest = 30 };
    PlusBtn.Image = "exp20181029Artboard51";
    MinusBtn.Image = "exp20181029Artboard52";
    switch (Device.RuntimePlatform)
    {
    case Device.UWP:
    case Device.Android:
    {
    PlusBtn.BackgroundColor = Color.Transparent;
    MinusBtn.BackgroundColor = Color.Transparent;
    break;
    }
    case Device.iOS:
    {
    PlusBtn.BackgroundColor = Color.Transparent;
    MinusBtn.BackgroundColor = Color.Transparent;
    break;
    }
    }
    Orientation = StackOrientation.Horizontal;
    PlusBtn.Clicked += PlusBtn_Clicked;
    MinusBtn.Clicked += MinusBtn_Clicked;
    Entry = new Entry { PlaceholderColor = Color.Gray, Keyboard = Keyboard.Numeric, WidthRequest = 30, BackgroundColor = Color.Transparent, FontSize = 15 };
    Entry.Keyboard = Keyboard.Numeric;
    Entry.Behaviors.Add(new NumericValidationBehavior());
    Entry.SetBinding(Entry.TextProperty, new Binding(nameof(Text), BindingMode.TwoWay, source: this));
    Entry.HorizontalTextAlignment = TextAlignment.Center;
    Entry.TextChanged += Entry_TextChanged;
    Children.Add(MinusBtn);
    Children.Add(Entry);
    Children.Add(PlusBtn);
    }
    private void Entry_TextChanged(object sender, TextChangedEventArgs e)
    {
    if (!string.IsNullOrEmpty(e.NewTextValue) && e.NewTextValue != ".")
    this.Text = int.Parse(e.NewTextValue);
    }

    private void MinusBtn_Clicked(object sender, EventArgs e)
    {
    if (Text > 0)
    Text--;
    }

    private void PlusBtn_Clicked(object sender, EventArgs e)
    {
    Text++;
    }

    }
    }


    When placing normally in the page I can access it and take the text property and use it in my Xaml.cs code. But in my case, I'm placing it inside a listview and as you know in listview the items are bindable I can't access it directly. In the regular stepper when it is placed in the listview we can use the "ValueChanged" method and can easily get the value by using e.NewValue in the "ValueChanged" method in the Xaml.cs file. Is there a way that I can add something to the CustomStepper class that can help me access the Text property and uses it in the Xaml.cs file? Thanks in advance










    share|improve this question


























      up vote
      1
      down vote

      favorite
      2









      up vote
      1
      down vote

      favorite
      2






      2





      I implemented this CustomStepper:



       using System;
      using Xamarin.Forms;

      namespace AppXamarin
      {
      public class CustomStepper : StackLayout
      {

      Button PlusBtn;
      Button MinusBtn;
      Entry Entry;

      public static readonly BindableProperty TextProperty =
      BindableProperty.Create(
      propertyName: "Text",
      returnType: typeof(int),
      declaringType: typeof(CustomStepper),
      defaultValue: 0,
      defaultBindingMode: BindingMode.TwoWay);

      public int Text
      {
      get { return (int)GetValue(TextProperty); }
      set { SetValue(TextProperty, value); }
      }
      public CustomStepper()
      {
      PlusBtn = new Button { WidthRequest = 30, HeightRequest = 30 };
      MinusBtn = new Button { WidthRequest = 30, HeightRequest = 30 };
      PlusBtn.Image = "exp20181029Artboard51";
      MinusBtn.Image = "exp20181029Artboard52";
      switch (Device.RuntimePlatform)
      {
      case Device.UWP:
      case Device.Android:
      {
      PlusBtn.BackgroundColor = Color.Transparent;
      MinusBtn.BackgroundColor = Color.Transparent;
      break;
      }
      case Device.iOS:
      {
      PlusBtn.BackgroundColor = Color.Transparent;
      MinusBtn.BackgroundColor = Color.Transparent;
      break;
      }
      }
      Orientation = StackOrientation.Horizontal;
      PlusBtn.Clicked += PlusBtn_Clicked;
      MinusBtn.Clicked += MinusBtn_Clicked;
      Entry = new Entry { PlaceholderColor = Color.Gray, Keyboard = Keyboard.Numeric, WidthRequest = 30, BackgroundColor = Color.Transparent, FontSize = 15 };
      Entry.Keyboard = Keyboard.Numeric;
      Entry.Behaviors.Add(new NumericValidationBehavior());
      Entry.SetBinding(Entry.TextProperty, new Binding(nameof(Text), BindingMode.TwoWay, source: this));
      Entry.HorizontalTextAlignment = TextAlignment.Center;
      Entry.TextChanged += Entry_TextChanged;
      Children.Add(MinusBtn);
      Children.Add(Entry);
      Children.Add(PlusBtn);
      }
      private void Entry_TextChanged(object sender, TextChangedEventArgs e)
      {
      if (!string.IsNullOrEmpty(e.NewTextValue) && e.NewTextValue != ".")
      this.Text = int.Parse(e.NewTextValue);
      }

      private void MinusBtn_Clicked(object sender, EventArgs e)
      {
      if (Text > 0)
      Text--;
      }

      private void PlusBtn_Clicked(object sender, EventArgs e)
      {
      Text++;
      }

      }
      }


      When placing normally in the page I can access it and take the text property and use it in my Xaml.cs code. But in my case, I'm placing it inside a listview and as you know in listview the items are bindable I can't access it directly. In the regular stepper when it is placed in the listview we can use the "ValueChanged" method and can easily get the value by using e.NewValue in the "ValueChanged" method in the Xaml.cs file. Is there a way that I can add something to the CustomStepper class that can help me access the Text property and uses it in the Xaml.cs file? Thanks in advance










      share|improve this question















      I implemented this CustomStepper:



       using System;
      using Xamarin.Forms;

      namespace AppXamarin
      {
      public class CustomStepper : StackLayout
      {

      Button PlusBtn;
      Button MinusBtn;
      Entry Entry;

      public static readonly BindableProperty TextProperty =
      BindableProperty.Create(
      propertyName: "Text",
      returnType: typeof(int),
      declaringType: typeof(CustomStepper),
      defaultValue: 0,
      defaultBindingMode: BindingMode.TwoWay);

      public int Text
      {
      get { return (int)GetValue(TextProperty); }
      set { SetValue(TextProperty, value); }
      }
      public CustomStepper()
      {
      PlusBtn = new Button { WidthRequest = 30, HeightRequest = 30 };
      MinusBtn = new Button { WidthRequest = 30, HeightRequest = 30 };
      PlusBtn.Image = "exp20181029Artboard51";
      MinusBtn.Image = "exp20181029Artboard52";
      switch (Device.RuntimePlatform)
      {
      case Device.UWP:
      case Device.Android:
      {
      PlusBtn.BackgroundColor = Color.Transparent;
      MinusBtn.BackgroundColor = Color.Transparent;
      break;
      }
      case Device.iOS:
      {
      PlusBtn.BackgroundColor = Color.Transparent;
      MinusBtn.BackgroundColor = Color.Transparent;
      break;
      }
      }
      Orientation = StackOrientation.Horizontal;
      PlusBtn.Clicked += PlusBtn_Clicked;
      MinusBtn.Clicked += MinusBtn_Clicked;
      Entry = new Entry { PlaceholderColor = Color.Gray, Keyboard = Keyboard.Numeric, WidthRequest = 30, BackgroundColor = Color.Transparent, FontSize = 15 };
      Entry.Keyboard = Keyboard.Numeric;
      Entry.Behaviors.Add(new NumericValidationBehavior());
      Entry.SetBinding(Entry.TextProperty, new Binding(nameof(Text), BindingMode.TwoWay, source: this));
      Entry.HorizontalTextAlignment = TextAlignment.Center;
      Entry.TextChanged += Entry_TextChanged;
      Children.Add(MinusBtn);
      Children.Add(Entry);
      Children.Add(PlusBtn);
      }
      private void Entry_TextChanged(object sender, TextChangedEventArgs e)
      {
      if (!string.IsNullOrEmpty(e.NewTextValue) && e.NewTextValue != ".")
      this.Text = int.Parse(e.NewTextValue);
      }

      private void MinusBtn_Clicked(object sender, EventArgs e)
      {
      if (Text > 0)
      Text--;
      }

      private void PlusBtn_Clicked(object sender, EventArgs e)
      {
      Text++;
      }

      }
      }


      When placing normally in the page I can access it and take the text property and use it in my Xaml.cs code. But in my case, I'm placing it inside a listview and as you know in listview the items are bindable I can't access it directly. In the regular stepper when it is placed in the listview we can use the "ValueChanged" method and can easily get the value by using e.NewValue in the "ValueChanged" method in the Xaml.cs file. Is there a way that I can add something to the CustomStepper class that can help me access the Text property and uses it in the Xaml.cs file? Thanks in advance







      xamarin.forms custom-renderer stepper






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 13:14

























      asked Nov 19 at 12:49









      mohammad anouti

      296




      296
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can create a property for EventHandlers. In this case, you would use the event modifier on the property to tell the program that the property is triggering an event. For example:



          private EventHandler onValueChangedEvent = null;

          public event EventHandler OnValueChanged
          {
          add
          {
          onValueChangedEvent = null;
          onValueChangedEvent = value;
          }
          remove
          {
          // Will show a warning. You can ignore it.
          onValueChangedEvent = null;
          }
          }

          private void Entry_TextChanged(object sender, TextChangedEventArgs e)
          {
          if (!string.IsNullOrEmpty(e.NewTextValue) && e.NewTextValue != ".")
          this.Text = int.Parse(e.NewTextValue);

          onValueChangedEvent?.Invoke(this, e);
          }


          You would then bind/assign an event handler in your xaml.cs code to the OnValueChanged property, which will get triggered when the value changes.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375020%2fxamarin-forms-custom-stepper-with-2-buttons-and-entry%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            You can create a property for EventHandlers. In this case, you would use the event modifier on the property to tell the program that the property is triggering an event. For example:



            private EventHandler onValueChangedEvent = null;

            public event EventHandler OnValueChanged
            {
            add
            {
            onValueChangedEvent = null;
            onValueChangedEvent = value;
            }
            remove
            {
            // Will show a warning. You can ignore it.
            onValueChangedEvent = null;
            }
            }

            private void Entry_TextChanged(object sender, TextChangedEventArgs e)
            {
            if (!string.IsNullOrEmpty(e.NewTextValue) && e.NewTextValue != ".")
            this.Text = int.Parse(e.NewTextValue);

            onValueChangedEvent?.Invoke(this, e);
            }


            You would then bind/assign an event handler in your xaml.cs code to the OnValueChanged property, which will get triggered when the value changes.






            share|improve this answer

























              up vote
              1
              down vote



              accepted










              You can create a property for EventHandlers. In this case, you would use the event modifier on the property to tell the program that the property is triggering an event. For example:



              private EventHandler onValueChangedEvent = null;

              public event EventHandler OnValueChanged
              {
              add
              {
              onValueChangedEvent = null;
              onValueChangedEvent = value;
              }
              remove
              {
              // Will show a warning. You can ignore it.
              onValueChangedEvent = null;
              }
              }

              private void Entry_TextChanged(object sender, TextChangedEventArgs e)
              {
              if (!string.IsNullOrEmpty(e.NewTextValue) && e.NewTextValue != ".")
              this.Text = int.Parse(e.NewTextValue);

              onValueChangedEvent?.Invoke(this, e);
              }


              You would then bind/assign an event handler in your xaml.cs code to the OnValueChanged property, which will get triggered when the value changes.






              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                You can create a property for EventHandlers. In this case, you would use the event modifier on the property to tell the program that the property is triggering an event. For example:



                private EventHandler onValueChangedEvent = null;

                public event EventHandler OnValueChanged
                {
                add
                {
                onValueChangedEvent = null;
                onValueChangedEvent = value;
                }
                remove
                {
                // Will show a warning. You can ignore it.
                onValueChangedEvent = null;
                }
                }

                private void Entry_TextChanged(object sender, TextChangedEventArgs e)
                {
                if (!string.IsNullOrEmpty(e.NewTextValue) && e.NewTextValue != ".")
                this.Text = int.Parse(e.NewTextValue);

                onValueChangedEvent?.Invoke(this, e);
                }


                You would then bind/assign an event handler in your xaml.cs code to the OnValueChanged property, which will get triggered when the value changes.






                share|improve this answer












                You can create a property for EventHandlers. In this case, you would use the event modifier on the property to tell the program that the property is triggering an event. For example:



                private EventHandler onValueChangedEvent = null;

                public event EventHandler OnValueChanged
                {
                add
                {
                onValueChangedEvent = null;
                onValueChangedEvent = value;
                }
                remove
                {
                // Will show a warning. You can ignore it.
                onValueChangedEvent = null;
                }
                }

                private void Entry_TextChanged(object sender, TextChangedEventArgs e)
                {
                if (!string.IsNullOrEmpty(e.NewTextValue) && e.NewTextValue != ".")
                this.Text = int.Parse(e.NewTextValue);

                onValueChangedEvent?.Invoke(this, e);
                }


                You would then bind/assign an event handler in your xaml.cs code to the OnValueChanged property, which will get triggered when the value changes.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 at 14:53









                Tom

                853512




                853512






























                    draft saved

                    draft discarded




















































                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


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




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375020%2fxamarin-forms-custom-stepper-with-2-buttons-and-entry%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    Origin of the phrase “under your belt”?