简体   繁体   中英

Get clicked button on lostfocus event

I have an UWP application which have a series of buttons with a click event. I also have a textbox that I need to keep focus on almost every click/touch on the app. So I set a resetFocus function like this:

public void resetfocus2(object sender, RoutedEventArgs e)
{
    Username.Focus(FocusState.Programmatic);
}

that triggers on every click outside this textbox.

This is the button in the XAML

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="4" Orientation="Vertical">
                <StackPanel Orientation="Horizontal">
                    <Button Tag="{Binding id}" Grid.Column="0" Padding="0" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="100" Background="White" Click="Meal1_Click">
                        <Image Width="100" Height="100" Source="{Binding idImg}" RelativePanel.AlignLeftWithPanel="True" />
                    </Button>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

My Question is:

How can I call the function normally called with the button click inside of this function ?

Since the lostfocus event happens before the click and the click function is never triggered.

UPDATE: This is the code that create the dialog from the button:

public async void Meal1_Click(object sender, RoutedEventArgs e)
    {
        ServiziSoapClient service = new ServiziSoapClient();
        var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
        var mealresponse = await service.SERVICE("PASSWORD", (sender as Button).Tag.ToString(), (string)localSettings.Values["stabilimento"]);

        var response = JsonValue.Parse(mealresponse.Body.SERVICE).GetObject();

        var notnull = 0;
        try
        {
            var temp = response.GetNamedArray("OPTION");
        }
        catch (Exception exc)
        {
            Debug.WriteLine("Exception " + exc.Message);
            notnull = 1;
        }

        JsonArray allergeni = null;
        var piatto = response.GetNamedObject("OPTION2");
        if (notnull == 0)
            allergeni = response.GetNamedArray("OPTION");

        var ingredienti = response.GetNamedString("OPTION3");

        var btn = sender as Button;
        var dialog = new ContentDialog()
        {
            Title = piatto.GetNamedString("descr"),
            //RequestedTheme = ElementTheme.Dark,
            //FullSizeDesired = true,
            MaxWidth = this.ActualWidth // Required for Mobile!
        };

        // Setup Content
        var panel = new StackPanel();

        var defaultImageUri = new Uri("ms-appx:///Images/ic_placeholder_primi.png");
        var bitmap = new BitmapImage();
        bitmap.ImageFailed += (s, ex) => bitmap.UriSource = defaultImageUri;
        bitmap.UriSource = new Uri("http://test-wsmensa.sinergest.it//WS/getImage.ashx?GETIN=B&&@pp2015Gi@BoS&ID_IMMAGINE=" + piatto.GetNamedNumber("idImg") + "&HEIGHT=100", UriKind.Absolute);

        panel.Children.Add(new Image
        {
            Height = 400,
            Width = 400,
            Source = bitmap
        });


        panel.Children.Add(new TextBlock
        {
            TextWrapping = TextWrapping.Wrap,
            FontSize = 20,
            Text = "Ingredienti: " + ingredienti
        });
        dialog.Content = panel;

        dialog.PrimaryButtonText = "Chiudi";
        // Show Dialog
        var result = await dialog.ShowAsync();
        UserName.Focus(FocusState.Programmatic);
    }

It seems you want the buttons won't get focus when you click or tap the buttons. If so you can set IsTabStop property to false to make buttons cannot receive input focus.

If IsTabStop is false , the control is excluded from tab navigation. In addition, if IsTabStop is false , the control cannot receive input focus. (If you try to set focus programmatically, by calling the Focus method, Focus returns false ).

So you can change your XAML code like:

<Button IsTabStop="False" Tag="{Binding id}" Grid.Column="0" Padding="0" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="100" Background="White" Click="Meal1_Click">

After this, the buttons won't get foucs and if your textbox has got focus, it will keep focus when users click or tap on the buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM