Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - ComboBox with Button items

I have this combobox and I wanted to add buttons as items in it. However, when I select the button from the combobox and click on the button, the action is not executed. The combobox's list falls instead. How should do this? If this is not possible, I think I'll just have to improvise. Suggestion will be appreciated. Thank you!

<ComboBox>
  <ComboBoxItem Name="Item1">
    <Button Name="Button1" Click="Button1_OnClick">first button</Button>
  </ComboBoxItem>
  <ComboBoxItem Name="Item2">
    <Button Name="Button2" Click="Button2_OnClick">second button</Button>
  </ComboBoxItem>
</ComboBox>
like image 758
Macho Gwapito Avatar asked Oct 16 '25 10:10

Macho Gwapito


1 Answers

You need ItemTemplate, like this:

<ComboBox x:Name="CB" Width="150" ItemsSource="{BindingItems}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Button Content="Click" Click="Button_Click" /> 
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

And you need the event handler:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Do something
}
like image 99
Thanos Markou Avatar answered Oct 19 '25 12:10

Thanos Markou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!