简体   繁体   中英

Click not working first time

I have a windows phone application.

Whenever I set a buttons visibility to collapse and I make it visible again I have to click on it twice to get it to fire the command it binds to. How can I make it fire after the first time again? It does not even hit any code at all on the first time. Both the visibility and button command is set through binding it to the view model properties.

I guess you are using MVVM light?

If so I had some similar problem when I used ICommand. If you instead only use the build-in relay command, then that solved the problem for me.

I think the problem comes from a "focus" issue. I suspect that the focus is confused by the change in visibility. The first click is then used to restore the focus.

To avoid this problem, I force the focus to be on the button just after changing the visibility

  private void RequestDialogBoxEvent(object sender)
    {

        this.DialogBox.Visibility = System.Windows.Visibility.Visible;
        this.buttonOK.Focus();
    }

with the XAML for the dialog box:

   <Border  x:Name="DialogBox" Background="Black" Grid.Row="1" Visibility="Collapsed" >
        <Grid  Margin="0,20" VerticalAlignment="Center" Background="Black">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock Text="Confirm ?"  />
            <Button x:Name="buttonOK" Content="OK" Grid.Row="1" Click="Button_Click" />
        </Grid>
    </Border>

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