【问题标题】:WPF - Combobox with button in ItemTemplate, click on SelectedItemWPF - ItemTemplate 中带有按钮的组合框,单击 SelectedItem
【发布时间】:2015-03-25 17:27:39
【问题描述】:

我有一个组合框,其中包含条目+操作列表作为文本和按钮:

[ Item #1 [Preview] v ]
| Item #2 [Preview]   |
| Item #3 [Preview]   |
| Item #4 [Preview]   |

问题是用户只能单击下拉列表上的按钮,而不能单击所选项目。基本上任何点击 ComboCox 都会打开下拉菜单。

有没有办法将下拉菜单限制为仅 V 按钮?或者以其他方式使所选项目中的按钮可点击?

这是我的 XAML

<ComboBox ItemsSource="{Binding LayoutsList}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" />
                <Button 
                    Command="{Binding PreviewCommand}" 
                    CommandParameter="{Binding}"
                    >Preview</Button>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

【问题讨论】:

    标签: wpf button combobox wpf-controls


    【解决方案1】:

    我认为这可能只有覆盖整个ComboBox.Template 才能实现。下拉菜单出现在 MouseDown 上,它似乎取消了项目内的任何后续事件处理程序,例如您的 Button.Click 事件,所以我认为除了覆盖模板之外没有其他方法。

    作为替代方案,您可以尝试使用PreviewMouseLeftButtonDown 来确定鼠标是否在[Preview] 按钮上,如果是,则设置e.Handled = true 并显示SelectedItem 的预览。不过,我不知道有什么简单的方法可以做到这一点,也不确定调查这条路线是否值得。

    【讨论】:

      猜你喜欢
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多