wpf组合框与复选框 - selecteditem [英] wpf combobox with checkbox - selecteditem

查看:435
本文介绍了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.

先感谢任何建议。

推荐答案

好吧,我已经尝试过使用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天全站免登陆