WPF ComboBox Selected Item 引用一个对象
WPF ComboBox Selected Item with reference to 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; }
}
-
设置所选项目
this.nodeWeek = new week() { val = times.GetIso8601WeekOfYear(DateTime.Now), name = "KW " + times.GetIso8601WeekOfYear(DateTime.Now).ToString() };
有办法解决这个问题吗?
所选项目必须始终是您的项目来源列表之一。您不能创建新对象并将它们分配给 SelectedItem
。 Combobox 只是比较对象引用而不是内容。
我正在尝试在组合框中定义选定的项目。如果我只是使用字符串来声明所选项目但如果使用对象则不行。
<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; }
}
- 设置所选项目
this.nodeWeek = new week() { val = times.GetIso8601WeekOfYear(DateTime.Now), name = "KW " + times.GetIso8601WeekOfYear(DateTime.Now).ToString() };
有办法解决这个问题吗?
所选项目必须始终是您的项目来源列表之一。您不能创建新对象并将它们分配给 SelectedItem
。 Combobox 只是比较对象引用而不是内容。