当绑定属性标记为只读时,禁用组合框下拉列表

时间:2022-11-06 20:03:12

I have a service which sets a property's ReadOnly attribute to true/ false in runtime, so the user will not be able to change the property value in propertyGrid.

我有一个服务,它在运行时将属性的ReadOnly属性设置为true / false,因此用户将无法更改propertyGrid中的属性值。

The service is useful when the property is being edit in a text editor. However, some of my editors are comboboxs. I want the dropdown to be disabled when I set the property to readonly- meaning the user will be able to see the selected item in the combobox, but when he clicks the arrow next to the combobox to see the options nothing will happend. this is not working with the current implementation. I guess I need to use a different attribute to the property. I've tried to change the "EditableAttribute" of the property but it doesn't work.... The code I am using to change readonly property:

在文本编辑器中编辑属性时,该服务很有用。但是,我的一些编辑器是组合框。我希望在将属性设置为readonly时禁用下拉列表 - 意味着用户将能够在组合框中看到所选项目,但是当他单击组合框旁边的箭头以查看选项时,不会发生任何事情。这不符合当前的实施。我想我需要对属性使用不同的属性。我试图更改属性的“EditableAttribute”但它不起作用....我用来更改readonly属性的代码:

        PropertyDescriptor descriptor = TypeDescriptor.GetProperties(obj)[property];
        ReadOnlyAttribute attribute = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
        FieldInfo fieldInfo = attribute.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
        fieldInfo.SetValue(attribute, value);

Any ideas?

有任何想法吗?

1 个解决方案

#1


0  

In standard WPF ComboBox, IsReadOnly has effect only when IsEditable = true, but does not prevent the user from selecting values. it just makes the editable text box read-only.

在标准WPF ComboBox中,IsReadOnly仅在IsEditable = true时有效,但不会阻止用户选择值。它只是使可编辑文本框为只读。

To completely prevent the control from selection you should use IsEnabled = false.

要完全阻止控件的选择,您应该使用IsEnabled = false。

#1


0  

In standard WPF ComboBox, IsReadOnly has effect only when IsEditable = true, but does not prevent the user from selecting values. it just makes the editable text box read-only.

在标准WPF ComboBox中,IsReadOnly仅在IsEditable = true时有效,但不会阻止用户选择值。它只是使可编辑文本框为只读。

To completely prevent the control from selection you should use IsEnabled = false.

要完全阻止控件的选择,您应该使用IsEnabled = false。