带有ItemTemplate的ComboBox,其中包含一个按钮 [英] ComboBox with ItemTemplate that includes a button
问题描述
所以,让我说一个带有自定义数据模板的ComboBox。数据模板中的一个项目是按钮:
So, lets say I have a ComboBox with a custom data template. One of the items in the data template is a button:
<ComboBox Width="150" ItemsSource="{Binding MyItems}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Button Content="ClickMe" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
这样做的问题是按钮用于点击,如果按钮。这意味着下拉不会消失,并且没有选择任何项。
The problem with this is that the button eats the click, and the item does not get selected if the button is selected. This means that the pull-down does not go away, and no item is selected.
我得到这是为什么发生。
I get WHY this is happening.
有办法解决它吗?可能是一种处理按钮点击的方式(我绑定到一个命令),并告诉它继续链,以便组合框也可以处理点击?
Is there a way to work around it? Possibly a way to process the button click (I am binding to a command) and tell it to continue up the chain so the combo box can also process the click?
注意:我在Silverlight中看到我的问题,但我猜想,与WPF可以看到完全相同的行为。
Note: I am seeing my problem in Silverlight, but I am guessing that the exact same behavior can be seen with WPF.
推荐答案
最好的办法是在按钮的命令中设置SelectedItem。
Your best bet would probably be to set the SelectedItem in the button's command.
这篇关于带有ItemTemplate的ComboBox,其中包含一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!