带有包含按钮的 ItemTemplate 的 ComboBox
所以,假设我有一个带有自定义数据模板的组合框。 数据模板中的项目之一是按钮:
<ComboBox Width="150" ItemsSource="{Binding MyItems}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Button Content="ClickMe" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
这样做的问题是该按钮会占用点击次数,并且如果选择该按钮,则不会选择该项目。 这意味着下拉菜单不会消失,并且不会选择任何项目。
我明白为什么会发生这种情况。
有办法解决吗? 可能有一种方法来处理按钮单击(我绑定到命令)并告诉它继续沿着链向上,以便组合框也可以处理单击?
注意:我在 Silverlight 中看到了我的问题,但我猜测在 WPF 中也可以看到完全相同的行为。
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?
Note: I am seeing my problem in Silverlight, but I am guessing that the exact same behavior can be seen with WPF.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,我明白了。 这完全是一个黑客,但它仍然允许我将命令绑定到按钮并继续具有用于选择项目的组合框行为:
并且在后面的代码中:
如果我真的愿意,我可以将 SelectedItem 和 IsDropDownOpen 绑定到我的 ViewModel 中的属性,但我决定反对将这种行为保留为 XAML 的黑客扩展,以保持我的 ViewModel 干净。
OK, I got it figured out. It is a total hack, but it still lets me bind my command to the button and continue to have Combo-box behavior for selecting the item:
And in the code behind:
If I really wanted to, I could bind the SelectedItem and IsDropDownOpen to properties in my ViewModel but I decided against it to keep this behavior as a hack extension of the XAML, in an effort to keep my ViewModel clean.
最好的选择可能是在按钮的命令中设置 SelectedItem。
Your best bet would probably be to set the SelectedItem in the button's command.
我发现了 MVVM 上下文的另一种可能性。 我为
ComboBox
使用了一个派生类,如果添加了一个派生自ButtonBase
的项目,我会附加到Click
事件来关闭组合框
。这适用于我的项目 - 但只是,因为项目本身是按钮,如果它们只包含按钮作为子元素,它就不起作用。
I found another possibility for the MVVM context. I used an derived class for
ComboBox
and if an item is adden which derives fromButtonBase
I attach to theClick
event to close theComboBox
.This works for my project - but just, because the items itself are buttons, it would not work if they just contain buttons as a child element.
我不知道是否有办法做你想做的事。 例如,如果您将
Button
放入ListBox
中,则会发生相同的行为 - 单击Button
不会导致其项目出现在列表框中。ListBox
被选中。 事实上,ItemsControl
中支持选择的任何控件都是这种情况。您也许可以对
Click
事件执行某些操作,并将其标记为未处理,以便它继续在可视化树中向上移动,但即便如此,我也不确定这是否有效。I don't know if there is a way to do what you want. If you were to put a
Button
in aListBox
, for example, the same behavior occurs - clicking theButton
does not cause its item in theListBox
to be selected. In fact, this is the case for any control in anItemsControl
that supports selection.You might be able to do something with the
Click
event and mark it as not handled so that it continues up the visual tree, but even then I'm not sure if that would work or not.