Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

[Feature] [ComboBox] Implement IsReadOnly property for ComboBox

Open Miniontoby opened this issue 1 month ago • 5 comments

Is your feature request related to a problem? Please describe.

The Feature Request is related to the problem that I am unable to set the IsReadOnly property on a ComboBox.

Not using axaml code and not using code-behind.

Describe the solution you'd like

I'd love to see a property named IsReadOnly like TextBox, CheckBox, DataGrid, and all of those other controls already have.

Usecase 1 - Enabled

  1. IsReadOnly is set to True
  2. User attempts to click on the ComboBox
  3. The ComboBox doesn't react

Usecase 2 - Disabled

  1. IsReadOnly is set to False
  2. User attempts to click on the ComboBox
  3. The ComboBox shows its dropdown

Usecase 3 - code-behind updating

  1. IsReadOnly is set to True
  2. User clicks on a row in a DataGrid above the field
  3. System sets IsReadOnly property to False
  4. User selects the correct value
  5. User clicks Save button
  6. System saves the changes and sets the IsReadOnly back to True

Describe alternatives you've considered

I've looked at IsEditable (#205), but it turns out that is used to change whether you can select the text inside the ComboBox instead of being the opposite of IsReadOnly.

I've also looked at IsEnabled, it works, but I've seen some strange behaviour with that property in other controls in the past, so I'd prefer to stay away from it. Also the naming of it wouldn't be inline with the other controls.

Additional context

I am attempting to rewrite a lot of WinForms projects to Avalonia and I'm just letting code convert WinForms to Avalonia. So'd prefer to not have to change the 'target' property if I don't have to.

Miniontoby avatar Nov 21 '25 10:11 Miniontoby

Maybe you can even file a PR to add this feature. You'd need to call AddOwner for this property of the TextBox and use TemplateBinding in the Styles.

https://docs.avaloniaui.net/docs/guides/custom-controls/defining-properties https://docs.avaloniaui.net/docs/guides/custom-controls/how-to-create-templated-controls#data-binding

timunie avatar Nov 24 '25 21:11 timunie

The thing is that I don't understand Avalonia good enough to uhm make a professional solution that would fit in with the rest of the code. I can try to come up with something DIY fashion, but like I dont think it will be quite on the same level as the rest of the code...

I'd love to help out, but I just dunno how to make this work, else I'd probably not even opened this issue in the first place. I'd have made a PR instead of an issue or just keep it for myself

Miniontoby avatar Nov 24 '25 22:11 Miniontoby

Oke,

I did look into it, but uhm I couldn't get it to work yet. IsReadOnly on ComboBox should just disable the allowance of opening the Dropdown/Popup... At least looking at the ComboBox code, I think that should be what would make sense.

^^ above comment is just for me to keep track of what I might try another time

Miniontoby avatar Nov 27 '25 13:11 Miniontoby

IsReadOnly means for me that the TextBox part cannot by typed. But user can still select different items. What you mean is more IsEnabled

We should follow WPF here imo: https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.combobox.isreadonly?view=windowsdesktop-10.0

timunie avatar Nov 27 '25 18:11 timunie

Oke welp, but like there isn't even a TextBox, only when the IsEditable is true, and then IsReadOnly would be useless.

But yeah IsEnabled for some controls just did strange behaviour... and to be honest it just doesn't sound logical...

But look:

  • The IsReadOnly on a DataGrid makes you unable to edit the datagrid as a user.
  • The IsReadOnly on a TextBox makes you unable to change the text
  • The IsReadOnly on a CheckBox makes you unable to change the state of the CheckBox
  • The IsReadOnly on a NumericUpDown makes you unable to go up and down

So why would the IsReadOnly on a ComboBox not make you unable to select a different option? Like unable to select a different option would make 100% sense for a ComboBox's IsReadOnly!!!

Miniontoby avatar Nov 27 '25 20:11 Miniontoby