I’m using MVVM to bind a ComboBox to an ObservableCollection in my WPF-application.
However, I need to make one or more items in this combobox “un-selectable”.
I should also mention, that the combobox is used in a DataGrid.
I suppose, that I could use some kind of ValueConverter. But I can’t figure out how.
Current XAML:
<DataGridTemplateColumn Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Type.Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel DataContext="{Binding DataContext.CurrentListUser,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">
<ComboBox ItemsSource="{Binding Types}"
DisplayMemberPath="Name"
SelectedValue="{Binding TypeId,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Id" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
You can wrap your data objects which you bind to combo box in dedicated view model classes. Add to view model class boolean property like IsReadOnly and then act appropriately depending on the value of IsReadOnly property. For example
In MainViewModel you can set IsReadOnly property for individual ArtistViewModel objects.
ComboBox is bound to Artists property of the MainViewModel.
For brevity I omitted implementation of INotifyPropertyChanged of VM classes.