ManuKILLED
ManuKILLED

Reputation: 71

WPF ComboBox Selected Item with reference to an object

I'm trying to define a selected item in a combobox. It working fine if I'm just using a String to declare the selected item but not if using an object.

<ComboBox HorizontalAlignment="Left"
  VerticalAlignment="Top" Width="81" materialDesign:HintAssist.Hint="Woche" Margin="10" 
  ItemsSource="{Binding weekSelection}" 
  DisplayMemberPath="name" 
  SelectedItem="{Binding nodeWeek, Mode=TwoWay}"  
SelectedValue="name" />

-

private week _nodeWeek;
public week nodeWeek
{
    get
    {
       return _nodeWeek;
    }
    set
    {
        _nodeWeek = value;
        RaisePropertyChanged("nodeWeek");
    }
}

-

 public class week
 {
    public int val { get; set; }

    public String name { get; set; }
 }

- setting the selected item

this.nodeWeek = new week() { val = times.GetIso8601WeekOfYear(DateTime.Now), name = "KW " + times.GetIso8601WeekOfYear(DateTime.Now).ToString() };

Is there a way to fix that?

Upvotes: 0

Views: 2989

Answers (1)

gomi42
gomi42

Reputation: 2519

The selected item must be always one of the list of your items source. You cannot create new objects and assign them to the SelectedItem. The Combobox simply compares object references not the content.

Upvotes: 2

Related Questions