0

I have a WPF ComboBox that I want to get the value from.

<ComboBox Name="ChoicesList" Grid.Column="2" Grid.Row="2" Margin="0,0,10,10">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ComboBoxItem Content="{Binding ChoiceName}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

I tried this (as seen here) to get it:

Bets.Add(new Bet() { Name = nameInput.Text, Amount = amount, Choice = (ChoicesList.SelectedValue as ComboBoxItem).Content.ToString() });

But every time I try to I get a System.NullReferenceException: 'Object reference not set to an instance of an object.' error. The list is clearly not empty.

So I tried another way to get it.

Bets.Add(new Bet() { Name = nameInput.Text, Amount = amount, Choice = ChoicesList.SelectedValue.ToString() });

I don't get an exception but I also did not get the data I want. I instead got PoolBet.MainWindow+Choice.

List initialization:

public ObservableCollection<Choice> Choices { get; set; } = new ObservableCollection<Choice>();

public class Choice
{
    public string ChoiceName { get; set; }
}

What am I doing wrong here?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
SpaceNerden
  • 72
  • 10
  • 1
    In your first attempt you're trying to cast `ChoicesList.SelectedValue` as a `ComboBoxItem`, though if you bound it to your ObservableCollection `Choices` the type of SelectedValue is `Choice` and not `ComboBoxItem`, resulting in the NullReferenceException. For the second attempt, you're using `ToString()` directly on your object though you didn't override the method, you have two options : 1. `Choice = (ChoicesList.SelectedValue as Choice).ChoiceName` and 2. add a ToString() method in your class `Choice` – Raphaël Aug 24 '21 at 10:17
  • Thank you very much! It now works flawlessly. Would set as answer if it was not a comment. – SpaceNerden Aug 24 '21 at 10:30
  • 1
    not directly regarding your question, but what is the point of Datatemplating the items, when the template is the default..? I think you might set the [DisplayMemberPath](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.itemscontrol.displaymemberpath?view=net-5.0)="ChoiceName" – dba Aug 24 '21 at 12:11
  • @SpaceNerden actually Ivan's answer is worthy of you taking a look in [MVVM](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/mvvm) for your SelectedValue can be at all time in a Property of your ViewModel – Raphaël Aug 24 '21 at 12:29

1 Answers1

1

View:

<ComboBox ItemsSource="{Binding Choices}" SelectedItem="{Binding SelectedChoice}"/>

View model:

public ObservableCollection<Choice> Choices { get; }

private Choice _selectedChoice;
public Choice SelectedChoice 
{
 get
 {
    return _selectedChoice;
 } 
 set
 {
    _selectedChoise = value;
    OnPropertyChanged();
 } 
}

Do not forget to implement INotifyPropertyChanged interface for your view model and to set view model as DataContext for view.

coding.monkey
  • 206
  • 1
  • 6
Ivan
  • 34
  • 4