1

I am trying to make a custom stepper to use in my listview such as this one Custom stepper

Any idea how to do this? Thanks.

Community
  • 1
  • 1
mohammad anouti
  • 171
  • 2
  • 18
  • What do you have already? Is this screenshot yours? What is the exact problem? – Gerald Versluis Nov 02 '18 at 07:54
  • This is a custom stacklayout with 2 buttons and 1 entry in the middle the problem is that I need to get the value of this entry when changed. For example, In the regular stepper, I can easily use ValueChanged method and handle the value. – mohammad anouti Nov 02 '18 at 08:06
  • If it is a custom control, you will need to add your own events so that the page can subscribe to those... – Depechie Nov 02 '18 at 10:25
  • Where exactly are you stuck? Post your current implementation. – memsranga Nov 03 '18 at 21:48
  • My implementation is the answer of "York Shen" I have the same custom class, I'm stuck at getting the value of the entry to use it in my code. I gave an example in the comment above that in the regular stepper you can easily use the "ValueChanged", but here I don't know how I can do that. – mohammad anouti Nov 05 '18 at 08:29
  • I used this answer: https://forums.xamarin.com/discussion/140026/stepper-in-xamarin-forms – mohammad anouti Nov 05 '18 at 15:45

1 Answers1

7

Solution 1:

A Stepper allows inputting a discrete value that is constrained to a range. You could display the value of the Stepper using data binding in a label as follows :

Define in XAML:

<StackLayout x:Name="Container">
    <Label BindingContext="{x:Reference stepper}" Text="{Binding Value}" />
    <Stepper Minimum="0" Maximum="10" x:Name="stepper" Increment="0.5" />
</StackLayout>

Solution 2:

You could create a BindableProperty to implement this function, for example:

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: 1,
          defaultBindingMode: BindingMode.TwoWay);

    public int Text
    {
        get { return (int)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public CustomStepper()
    {
        PlusBtn = new Button { Text = "+", WidthRequest = 40, FontAttributes = FontAttributes.Bold, FontSize = 15 };
        MinusBtn = new Button { Text = "-", WidthRequest = 40, FontAttributes = FontAttributes.Bold, FontSize = 15 };
        switch (Device.RuntimePlatform)
        {

            case Device.UWP:
            case Device.Android:
                {
                    PlusBtn.BackgroundColor = Color.Transparent;
                    MinusBtn.BackgroundColor = Color.Transparent;
                    break;
                }
            case Device.iOS:
                {
                    PlusBtn.BackgroundColor = Color.DarkGray;
                    MinusBtn.BackgroundColor = Color.DarkGray;
                    break;
                }
        }

        Orientation = StackOrientation.Horizontal;
        PlusBtn.Clicked += PlusBtn_Clicked;
        MinusBtn.Clicked += MinusBtn_Clicked;
        Entry = new Entry
        {
            PlaceholderColor = Color.Gray,
            Keyboard = Keyboard.Numeric,
            WidthRequest = 40, BackgroundColor = Color.FromHex("#3FFF")
        };
        Entry.SetBinding(Entry.TextProperty, new Binding(nameof(Text), BindingMode.TwoWay, source: this));
        Entry.TextChanged += Entry_TextChanged;
        Children.Add(PlusBtn);
        Children.Add(Entry);
        Children.Add(MinusBtn);
    }

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

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

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

}

For more detailed information, please refer to the following documents:

Update:

In the CustomStepper class, the Entry value is binding with the Text property, so you could get the value of the entry via customStepper.Text.

For example:

<local:CustomStepper x:Name="MyCustomerStepper"/>

You could get its Entry value in your xaml.cs file via:

var yourCustomerStepperEntryValue = MyCustomerStepper.Text.ToString();
Community
  • 1
  • 1
York Shen
  • 9,014
  • 1
  • 16
  • 40
  • This exactly what I'm doing, I need to get this value of the entry to use it in the XAML.cs class. – mohammad anouti Nov 05 '18 at 08:26
  • Do you have any solutions? – mohammad anouti Nov 08 '18 at 08:48
  • @mohammadanouti, you could get it via using `var yourStepperValue = stepper.Value;`. – York Shen Nov 08 '18 at 08:59
  • I know well about custom renders and how to use them in XAML and so on, but the problem is I don't know how to implement it from scratch so please can you share me some code how can in XAML take the Entry of the custom Stacklayout I created to use it in the Xaml.cs file – mohammad anouti Nov 12 '18 at 15:07
  • please reply. Thank you in advance – mohammad anouti Nov 14 '18 at 10:24
  • @mohammadanouti, Coul you please elaborate a bit more about the feature you want to achieve? :) – York Shen Nov 14 '18 at 12:24
  • 1
    @mohammadanouti, I have updated my answer, please check it. – York Shen Nov 14 '18 at 13:12
  • Thank you, it is exactly what you need to do when you are using it normally on the page, but the thing is that I'm using it in a listview and as u know items in listview can't access it directly. Normal Stepper in a listview you can access it by using "ValueChanged" Method and can easily get the value by using e.NewValue in the "ValueChanged" method in the Xaml.cs file. But in my case, I can't access the CustomStepper directly from the listview, so is there a way to get the value of the text when this CustomStepper is placed in the listview. I hope you get my point and help me to solve it. – mohammad anouti Nov 16 '18 at 11:13
  • I'm really thankful for your help and support, but I need your response as soon as possible. – mohammad anouti Nov 19 '18 at 09:22
  • @mohammadanouti, sorry for my late response, for your question, I think you could refer to this: https://stackoverflow.com/questions/53189391/when-i-click-the-event-on-outside-of-listview-switch-button-or-inside-of-listvie/53201677#53201677 – York Shen Nov 19 '18 at 11:44
  • @mohammadanouti, I haven't test it on my side, but I think they are the same issue – York Shen Nov 19 '18 at 11:45
  • I tried before something similar to this, I placed a TapGestureRecognizer, ClickedGestureRecognizer and SwipeGestureRecognizer for the CustomStepper since its a stacklayout so every time I change the value it will be saved, but it is not entering any of these when tapping or clicking on the buttons or the entry :( – mohammad anouti Nov 19 '18 at 12:15
  • @mohammadanouti, I haven’t thought of a solution for the time being, if this is urgent for you, I suggest you to raise a new question in StackOverflow. Sorry for any inconvenience – York Shen Nov 19 '18 at 12:38
  • No problem, Thank you for your help. Ok i will – mohammad anouti Nov 19 '18 at 12:47
  • Thanks for this idea :) – Hemant Ramphul Nov 02 '19 at 20:34