XAML ReadOnly ComboBox

To configure ReadOnly ComboBox in XAML (WPF), you need to configure ComboBox and TextBox , showing only one of them according to the pair of IsReadOnly / IsEditable properties that must exist on your ViewModel . Note that in this example, " UserNVL " must exist in the resources, and it must be a collection of NameValueList , which allows us to convert ID to names. In this case, the RecipientID is the key for the username. Note that the VisibilityConverter must also exist in the resources, and the standard BooleanToVisibilityConverter must exist.

Gosh! It was so hard to find that I had to do it myself. This allows the user to select the contents of the text field. A disabled ComboBox will never let you do this.

+6
wpf readonly xaml combobox
source share
5 answers

There are two properties named IsHitTestVisible and IsTabVisible. the first makes the control deaf for mouse events, and the second for keyboard events. This may help you, as it will not give the disabled look in your combo box, but you will only be able to create a read field. Source: - http://www.telerik.com/community/forums/wpf/combobox/isreadonly-does-seem-to-work.aspx

+4
source share
 <DockPanel> <TextBlock Text="Recipient" Margin="6,9,3,6" HorizontalAlignment="Right"/> <ComboBox x:Name="RecipientID" ItemsSource="{Binding Source={StaticResource UserNVL}}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding Path=RecipientID}" Height="20" Margin="6,6,0,6" MinWidth="200" HorizontalAlignment="Left" IsEditable ="True" Visibility="{Binding Path=IsEditable, Converter={StaticResource VisibilityConverter}}"/> <TextBox x:Name="RecipientName" Text="{Binding ElementName=RecipientID, Path=Text}" Margin="6,6,0,6" MinWidth="200" HorizontalAlignment="Left" Style="{StaticResource textBoxInError}" Visibility="{Binding Path=IsReadOnly, Converter={StaticResource VisibilityConverter}}"/> </DockPanel> 
+3
source share

Why not just set IsEnabled = false?

0
source share

If IsEnabled is set to false, the Combobox is almost unreadable. I found a suitable solution:

  • combobox and text field (formed as readonly) are in the same grid position
  • combobox spans the next column to get an extra width of 15, so the dropdown button is visible.
  • textbox.IsVisible is bound to combobox.IsEnabled with bool for the visibility converter.
  • textbox.Text is associated with combobox.SelectedItem (in my case, it is strongly typed, so I'm actually attached to .DisplayText)
0
source share

I think that it will be much easier and more practical for you to create a class to extend the ComboBox class in the simplest way:

  • override the OnSelectionChanged Combobox method to check the IsReadOnly property before allowing base.OnSelectionChanged (e) to run.

Thus, you just need to set the ComboBox.IsReadOnly property to True. No big xaml to write everywhere ...

0
source share

All Articles