简体   繁体   中英

WPF Button inside ListView not firing

I have a very simple question for WPF experts.

I start with XAML the code:

<ListView Name="ListViewRunner" ItemsSource="{Binding Path=CurrentPrices}" >
  <GridView>
     <!-- Many GridViewColumn that display values --> 

     <GridViewColumn Header="Chart" Width="70" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="btnDisplayChart" Content="Chart"
                                        Click="DisplayChart_Click"
                                        Tag="{Binding ID}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
       </GridViewColumn>
  </GridView>
</Listview>

CurrentPrices is an asynchronous ObservableCollection and a separate thread updates the other columns every 200ms. Now when I click the button as defined above nothing appears to happen (like I didn't click the button), but if I increase the sleeping time of the other thread (ie 2000ms) the code behind correctly processes the event.

Could you please point me to the right direction?

Here is the code that update the collection

private void getRemotePrices(){
            while(update)
            {
                lock (boccaciccio)
                {
                    MarketPrices marketprices = bf.GetMarketPrices(market.ID);
                    market.UpdateMarketPrices(marketprices);

                    lastPrices.Clear();
                    foreach (var runnerprice in market.ADATA)
                    {
                        lastPrices.Add(runnerprice);
                    }
                    notifyObservers(lastPrices); //updates every subscriber
                    Thread.Sleep(200);
                }
            }
        }

I think the problem is refreshing the list. The update is faster than you can click. You shouldn't clear the list, but sync it.

Clicking a button includes mouse down and then mouse up activities. Both activities should be done when the mouse above the same button instance. When you refresh the items in the GridView too quickly, your mouse down activity happens on one button instance, then the list refreshed and the button instance under your mouse is replaced by other button instance, so mouse up activity done on other button instance.

It the same as if you press mouse button down while the mouse on one button, then move the mouse to other button and then releasing the mouse button. No click event will be fired.

You can change the click event to mouse down or mouse up event and ensure firing , but I don't sure it would be the right solution from the design point of view. If you need design suggestions you should supply more information.

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