A button for each items in the combobox not firing

devLearning 1 Reputation point
2022-05-02T19:07:33.26+00:00

Hi,

I have a combobox that has a list of items. The combobox is binded to an ObservableCollection. Each item has a button on the right. This purpose of the button is for user to click and delete the item. Currently, when I click the button, nothing gets fire. Please see xaml attached. 198320-filters.xml

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
789 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 47,336 Reputation points Microsoft Vendor
    2022-05-16T10:08:07.867+00:00

    I can't reproduce your problem. You could try to refer to the example below.
    MainWindow.xaml:

    <Window.DataContext>  
        <local:ViewModel />  
    </Window.DataContext>  
    <Window.Resources>  
        <DataTemplate x:Key="ComboBoxItemTemplate"  >  
            <Grid  Width="{Binding ElementName=cb,Path=Width }">  
                <Grid.ColumnDefinitions>  
                    <ColumnDefinition Width="8*"/>  
                    <ColumnDefinition Width="2*"/>  
                </Grid.ColumnDefinitions>  
                <TextBlock x:Name="EntityName" Text="{Binding Name}" />  
                <Image Visibility="{Binding ISShow}" Grid.Column="1"  Source="delete.png"  HorizontalAlignment="Center" >  
    
                    <Image.InputBindings>  
                        <MouseBinding Gesture="LeftClick"   
                                Command="{Binding DataContext.MyCommand,   
                                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"   
                                CommandParameter="{Binding}"/>  
                    </Image.InputBindings>  
                </Image>  
            </Grid>  
        </DataTemplate>  
    </Window.Resources>  
    <Grid>  
        <ComboBox Name="cb" ItemsSource="{Binding Path=Books}" Width="200" Height="40"  
                  ItemTemplate="{StaticResource ComboBoxItemTemplate}"  
                  SelectedValue="{Binding SelectedItem ,Mode=TwoWay}" >  
        </ComboBox>  
    </Grid>  
    

    MainWindow.xaml.cs:

    202209-2.txt

    The result:
    202200-image.png
    202302-9.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html


  2. devLearning 1 Reputation point
    2022-05-19T16:17:28.253+00:00

    Hi @Hui Liu-MSFT ,

    Thanks for the solution.

    I will take a look and apply your idea into my solution. I am very appreciate your help on this.

    Thanks,
    devLearning

    0 comments No comments