xamarinfreshmvvm

How to pass the bound property with command to modify it


i have a page with multiple labels that each have a different property bound to it and when tapped open up a prompt to modify their value. I can't figure out how to pass that property with the command so i can modify it and use this same command for all labels.

In ContentPage:

                <Label x:Name="lblLevel" Text="{Binding Level}" FontSize="Large">
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer
                            Command="{Binding InputPopup}"
                            CommandParameter="{Binding Source={x:Reference lblLevel}, Path=Text}" />
                    </Label.GestureRecognizers>
                </Label> 

Command:

    public ICommand InputPopup => new Command(
        async () => {
            PromptResult pResult = await UserDialogs.Instance.PromptAsync(new PromptConfig
            {
                InputType = InputType.Name,
                OkText = "Confirm",
                Title = "New value",
                CancelText = "Cancel",
                MaxLength = 1,
            });
            if (pResult.Ok && !string.IsNullOrWhiteSpace(pResult.Text))
            {
//Todo: PropertyX (Level) = pResult.Text
            }

        }
    );

Thank you


Solution

  • So after trying a couple different things that i couldn't get to work i ended up using a switch, it's not pretty but atleast it works.

    Command (in FreshMVVM ViewModel):

    public ICommand EditValuePopupCommand => new Command<string>(
        async (param) =>
        {
            PromptResult pResult = await UserDialogs.Instance.PromptAsync(new PromptConfig
            {
                InputType = InputType.Name,
                OkText = "Confirm",
                Title = $"Edit {param}",
                CancelText = "Cancel",
                MaxLength = 2,
            });
            if (pResult.Ok && !string.IsNullOrWhiteSpace(pResult.Text))
            {
                switch (param)
                {
                    case "Level": Level = int.Parse(pResult.Text); break;
                    case "Strength": Strength = "Str: " + pResult.Text; break;
                    case "Dexterity": Dexterity = "Dex: " + pResult.Text; break;
                    case "Constitution": Constitution = "Con: " + pResult.Text; break;
                    case "Wisdom": Wisdom = "Wis: " + pResult.Text; break;
                    case "Intelligence": Intelligence = "Int: " + pResult.Text; break;
                    case "Charisma": Charisma = "Cha: " + pResult.Text; break;
                    case "AttackModifier": AttackModifier = "Attack: " + pResult.Text; break;
                    case "SpellAttackModifier": SpellAttackModifier = "Spell Attack: " + pResult.Text; break;
                    case "SecondaryAttackModifier": SecondaryAttackModifier = "Second Attack: " + pResult.Text; break;
                    case "SaveDC": SaveDC = "Second Attack: " + pResult.Text; break;
    
                    default:
                        Console.WriteLine("Default case");
                        break;
                }
            }
        }
    );
    

    XAML:

    <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
                        <Frame Style="{StaticResource DefaultFrameStyle}">
                            <Label x:Name="lblStrength" Text="{Binding Strength}">
                                <Label.GestureRecognizers>
                                    <TapGestureRecognizer
                                        Command="{Binding EditValuePopupCommand}"
                                        CommandParameter="Strength" />
                                </Label.GestureRecognizers>
                            </Label>
                        </Frame>
    
                        <Frame Style="{StaticResource DefaultFrameStyle}">
                            <Label x:Name="lblDexterity" Text="{Binding Dexterity}">
                                <Label.GestureRecognizers>
                                    <TapGestureRecognizer
                                        Command="{Binding EditValuePopupCommand}"
                                        CommandParameter="Dexterity" />
                                </Label.GestureRecognizers>
                            </Label>
                        </Frame>