WPF - 带有按钮项目的组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29840901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - ComboBox with Button items
提问by Macho Gwapito
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>
回答by Thanos Markou
You need ItemTemplate, like this:
你需要 ItemTemplate,像这样:
<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
}

