Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

How to navigate the current Page?

$
0
0

Hello guys!
Here is my problem: I'm trying to create a kind of validator for an entry. The entry has to be an int. If the user's entry is not an int, I wanna display an error text "the entry has to be an int".
My code looks like this:

        var intNum = new Entry();
        intNum.Keyboard = Keyboard.Numeric;
        intNum.TextChanged += OnTextChanged;

        var label = new Label();
        label.Text = "some text";

        var error = new Label();
        error.Text = "Input an integer please";
        error.TextColor = Color.Red;
        error.IsVisible = false;


        var superGrid = new Grid();
        superGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
        superGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });

        var grid = new Grid();
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(20) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(30) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(150, GridUnitType.Star) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(20) });


        grid.Children.Add(label, 1, 0);
        grid.Children.Add(new Entry(), 3, 0);

        superGrid.Children.Add(grid, 0, 0);
        superGrid.Children.Add(error, 0, 1);

I would like to have an event handler on the Entry (OnTextChanged for instance) that permits me to access error in order to set error.IsVisible=true;. So far I ended up with something like

        var txt = sender as Entry;
        var superGrid = txt.Parent.Parent as Grid;

But I don't get how to access the wanted child and this looks like kind of rubbish to me except that I have no clue how to proceed otherwise.

Any ideas/solutions?


Viewing all articles
Browse latest Browse all 89864

Trending Articles