TextProperty': derive content

Eduard Kaufmann 211 Reputation points
2023-01-13T07:30:09.3266667+00:00
Hi,
thanks for your time ... I'm having a blackout !
environment: VS2022, .net7, MAUI

How can I derive the content of the TextProperty: in this case 'Siou Blanc'?

I used n variations without success ... must be blind!
string location = Button.TextProperty.ToString();
// result: location = 'Text'
ed
        <Button
        Grid.Row="1"
        Grid.Column="0"
        Margin="8"
        Command="{Binding MCommand}"
        IsEnabled="{Binding IsNotBusy}"
        Style="{StaticResource ButtonOutline}"
        Text="Siou Blanc"/>
        
    </VerticalStackLayout>
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,589 questions
{count} votes

Accepted answer
  1. Viorel 109.9K Reputation points
    2023-01-13T09:25:35.0766667+00:00

    Try assigning a name: <Button x:Name="button1" ....

    In the associated code file: string location = button1.Text.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 64,011 Reputation points Microsoft Vendor
    2023-01-20T05:49:57.2166667+00:00

    Hello,

    You can do this by adding CommandParameter="{x:Reference button1}" in the Button like following code.

        <Button x:Name="button1"
                Margin="8"
                Command="{Binding VARhCommand}"
                CommandParameter="{x:Reference button1}"
                IsEnabled="{Binding IsNotBusy}"
                Style="{StaticResource ButtonOutline}"
                Text="VAR-Haute"/>
    
    

    Then add an attribute in your VARhAsync of HikesViweModel.cs. You can get the button text in the ViewModel.

        [RelayCommand]
        async Task VARhAsync(Button btn)    {
            string location = btn.Text;
        }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.