简体   繁体   中英

WP7 Button inside ListBox only "clicks" every other press

I have a button defined inside of a DataTemplate for my list box.

<phone:PhoneApplicationPage.Resources>        
    <DataTemplate x:Key="ListTemplate">
        <Grid Margin="12,12,24,12">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Button Grid.Column="0" Name="EnableDisableButton" 
                    Click="EnableDisableButton_Click" 
                    BorderBrush="Transparent">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Image Grid.Row="0" Source="\Images\img.dark.png" Width="48" Height="48" Visibility="{StaticResource PhoneDarkThemeVisibility}" />
                    <Image Grid.Row="0" Source="\Images\img.light.png" Width="48" Height="48" Visibility="{StaticResource PhoneLightThemeVisibility}" />
                    <Rectangle Grid.Row="1" Width="48" Height="8" Fill="{Binding CurrentColor}" RadiusX="4" RadiusY="4" />
                </Grid>
            </Button>

            <Grid Grid.Column="1">
              <... more stuff here ...>
            </Grid>
        </Grid>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

What I'm seeing is that the first time I press the button, the Click event fires. The second time I press it, it does not fire. Third press, fires. Fourth press, does not fire. Etc. Originally I had it bound to a command but that was behaving the same way. (I put a Debug.WriteLine() in the event handler so I know when it fires.)

Any ideas? It's really odd that the click event only fires every other time.

Try one the following two "workarounds":

  1. In your click event callback set the focus back to the page: this.Focus();

  2. In your click event callback set the SelectedItem of your ListBox to null: myListBox.SelectedItem = null;

I think one of the two should work...

click event callback = EnableDisableButton_Click method

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