I am working on a configuration editor and I was having issues binding ObservableCollection
to combobox items in XAML.
My setup:
- I am using Prism
- I am also using MaterialDesignThemes in the project
Here is the error message:
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property. null BindingExpression:Path=(0); DataItem='ComboBox' (Name='EncryptionSelector'); target element is 'ComboBox' (Name='EncryptionSelector'); target property is 'Name' (type 'String')
What am I doing wrong here to get this error?
My code:
XAML:
<ComboBox x:Name="EncryptionSelector" Grid.Column="1"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center"
VerticalAlignment="Center"
Width="Auto" Height="25"
Background="White"
Padding="0,0,2,0"
Margin="80,0,15,0" ItemsSource="{Binding EncTypeOptions}"
DisplayMemberPath="DisplayName" SelectedValuePath="Tag"
SelectedValue="{Binding SelectedEncType, Mode=TwoWay, TargetNullValue=''}"/>
View model:
public ObservableCollection<ComboOptions> EncTypeOptions { get; } =
new ObservableCollection<ComboOptions>()
{
new ComboOptions() { DisplayName = "-- Select an Encryption Type --", Tag = "" },
new ComboOptions() { DisplayName = $"{EncryptionMode.AES}", Tag = "AES" },
new ComboOptions() { DisplayName = $"{EncryptionMode.RSA}", Tag = "RSA" },
new ComboOptions() { DisplayName = $"{EncryptionMode.Hybrid}", Tag = "Hybrid" },
};
private string _selectedEncType = "";
public string SelectedEncType
{
get => _selectedEncType;
set => SetProperty(ref _selectedEncType, value);
}
Class:
public class ComboOptions
{
public string DisplayName { get; set; } = string.Empty;
public string Tag { get; set; } = string.Empty;
}
Here is the XAML binding failure:
ComboBox, Name='EncryptionSelector' (0) ComboBox.(AutomationProperties.Name), Name='EncryptionSelector' String Value 'null' cannot be assigned to property ComboBox.Name, Name='EncryptionSelector' (type String).