带有复选框的 wpf 组合框 - selecteditem [英] wpf combobox with checkbox - selecteditem

查看:24
本文介绍了带有复选框的 wpf 组合框 - selecteditem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的组合框,带有复选框作为项目.如何防止实际选择项目.用户应该只能选中或取消选中复选框?

I have a simple ComboBox with CheckBoxes as Items. How can I prevent the actual selection of the items. The user should only be able to check or uncheck the checkboxes?

目前,如果我点击一个元素(不是内容或支票本身),它就会被选中.问题在于:ComboBox 的 TextProperty 绑定到一个值,该值显示选中项的名称.但如果一个 ComboBoxItem 被选中,则显示的文本将成为所选项目的 ViewModel 的值.

Currently if I click on an element (not on the content or the check itself) it becomes selected. The problem with this is: The TextProperty of the ComboBox is bound to a value which displays the names of the checked items. But if one ComboBoxItem becomes selected the displayed text becomes the value of the ViewModel of the selected item.

预先感谢您的任何建议.

Thanks in advance for any suggestion.

推荐答案

好的,我之前已经尝试过使用 GetBindingExpression(...).UpdateTarget() 因为我的 TextProperty 已绑定但什么也没发生.此功能只有在布局更新后才会生效.所以结果:

Ok, I already tried before to use GetBindingExpression(...).UpdateTarget() because my TextProperty is bound but nothing happend. This function will only have an effect after the layout was updated. So the result:

/// <summary>
/// Prevents the selection of an item and displays the result of the TextProperty-Binding
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SeveritiesComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox box = sender as ComboBox;

    if (box == null)
        return;

    if (box.SelectedItem != null)
    {
        box.SelectedItem = null;

        EventHandler layoutUpdated = null;

        layoutUpdated = new EventHandler((o, ev) =>
        {
            box.GetBindingExpression(ComboBox.TextProperty).UpdateTarget();
            box.LayoutUpdated -= layoutUpdated;
        });

        box.LayoutUpdated += layoutUpdated;
    }
}

这篇关于带有复选框的 wpf 组合框 - selecteditem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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