WPF ComboBox Selected Item with reference to an object

对着背影说爱祢 提交于 2020-01-25 07:15:09

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/48008220/wpf-combobox-selected-item-with-reference-to-an-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!