tags:

views:

170

answers:

1

I can get a button to appear and be clickable in the drop down list of a combo box, but I cannot get the selected combo box item (the drop list is closed) to have the button be clickable. It always skips the button click and just opens the drop down list. I basically want the Button_Click event handler that I setup to be called once it is clicked. Here is my sample combo box that shows the button but is not clickable once it is in the selected item:

<ComboBox x:Name="MyCombo" Width="200" Height="30" ItemsSource="{Binding ListCombo}">
        <ComboBox.Resources>
            <DataTemplate DataType="{x:Type local:ComboItemClass}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=SampleText}" Width="120" />
                    <Button Width="20" Content="..." Click="Button_Click"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.Resources>
    </ComboBox>
+3  A: 

Putting buttons inside comboboxes is one of those really cool features that we now CAN do in WPF that we (me included) get really excited about, before we stop to consider if we SHOULD do this.

Having a button inside a combobox makes it very easy to confuse the heck out of your user. I would recommend that you bind the data from your comobox listitems to a button outside of the combobox, where your user will expect it. That way, you can still change the end result of the button being pressed by selecting an item from the combobox.

EDIT:

If you have the space for it, a listbox would work perfectly for what you want to do.

    <ListBox>
        <ListBoxItem>
            <StackPanel Height="34" HorizontalAlignment="Left" Margin="12,16,0,0"  VerticalAlignment="Top" Width="430" Orientation="Horizontal">
                <Button Content="Edit"  />
                <Button Content="Delete"  />
                <TextBlock Text="Port Information here" VerticalAlignment="Center" Margin="20,0" />
            </StackPanel>

        </ListBoxItem>
        <ListBoxItem>
            <StackPanel Height="34" HorizontalAlignment="Left" Margin="12,16,0,0"  VerticalAlignment="Top" Width="430" Orientation="Horizontal">
                <Button Content="Edit"  />
                <Button Content="Delete"  />
                <TextBlock Text="Port Information here" VerticalAlignment="Center" Margin="20,0" />
            </StackPanel>

        </ListBoxItem>
        <ListBoxItem>
            <StackPanel  Height="34" HorizontalAlignment="Left" Margin="12,16,0,0"  VerticalAlignment="Top" Width="430" Orientation="Horizontal">
                <Button Content="Edit"  />
                <Button Content="Delete"  />
                <TextBlock Text="Port Information here" VerticalAlignment="Center" Margin="20,0" />
            </StackPanel>

        </ListBoxItem>
Stewbob
The way I am using this combo box is to show a list of configured ports. I want two buttons inside so the user can edit or delete a port. I wanted to place the buttons on the combo box (while the dropdown is closed) so the user can just click there rather than open the combo box and then click edit or delete.
Travyguy9
You will have to hijack the click event for the combobox, then write a hit-testing routine to see if the click occurred over your button. If it did, then don't allow the combobox to continue with the DropDown method, but instead route control to the button code. If the mouse was not over the button, then the Click event for the combobox should proceed normally. This might get kind of messy.
Stewbob
Oops...No 'Click' event for WPF combobox. This could get really messy.
Stewbob
Hmm I can't figure out how to find that button control once the combobox is clicked. All else fails, I can just use what you said first off. Thanks.
Travyguy9