Have a clickable button on top of a combobox in WPF

倖福魔咒の 提交于 2020-01-24 09:59:26

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/2740732/have-a-clickable-button-on-top-of-a-combobox-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!