0

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).

7
  • 1
    Please edit your question to remove the redundant indentation - for both the ViewModel and the XAML that indentation means scrolling is required. Commented Oct 10 at 13:28
  • @JonSkeet is this okay? Commented Oct 10 at 13:33
  • 1
    That's much better, yes. There's no need to reduce the indentation lvel level to a single character, for future reference - but make sure that each snippet actually starts on the left hand side, so it's not wasting space. Commented Oct 10 at 14:09
  • 1
    (Your ComboOptions) "Tag" is also a (key) FrameworkElement property; which is an object. You're digging yourself a hole, IMO. Commented Oct 10 at 14:24
  • 1
    At first glance, your code appears error-free. My advice: create a minimal SOLUTION example that reproduces the bug, so it can be easily downloaded and run. A GitHub repository would be ideal. Commented Oct 10 at 20:34

1 Answer 1

1

Rename the ComboBox x:Name Avoid the bare word “EncryptionSelector” since there’s a known collision pattern with MaterialDesign’s accessibility hints.

<ComboBox x:Name="EncryptionSelectorCombo"
          ItemsSource="{Binding EncTypeOptions}"
          DisplayMemberPath="DisplayName"
          SelectedValuePath="Tag"
          SelectedValue="{Binding SelectedEncType, Mode=TwoWay, TargetNullValue=''}" />
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Rahman…I will give it a try and see what happens

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.