在WPF中的组合框顶部有一个可单击的按钮 [英] Have a clickable button on top of a combobox in WPF

查看:44
本文介绍了在WPF中的组合框顶部有一个可单击的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在组合框的下拉列表中显示一个按钮并单击它,但是我无法使所选组合框项目(下拉列表处于关闭状态)使该按钮可单击.它总是跳过单击按钮,只是打开下拉列表.我基本上希望单击后将调用我设置的Button_Click事件处理程序.这是我的示例组合框,其中显示了该按钮,但是一旦位于所选项目中便无法单击:<​​/p>

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>

推荐答案

在组合框内放置按钮是我们现在可以在WPF中做的那些非常酷的功能之一,而在停止之前,我们(包括我在内)对此感到非常兴奋考虑我们是否应该这样做.

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.

在组合框内有一个按钮,可以很容易地使用户困惑.我建议您将comobox列表项中的数据绑定到组合框外部的按钮上,用户会在该按钮上找到它.这样,您仍然可以通过从组合框中选择一个项目来更改按下按钮的最终结果.

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.

如果有足够的空间,一个列表框将非常适合您想要做的事情.

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>

这篇关于在WPF中的组合框顶部有一个可单击的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆