WPF ComboBox选定的项目参考一个对象 [英] WPF ComboBox Selected Item with reference to an object

查看:97
本文介绍了WPF ComboBox选定的项目参考一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在组合框中定义一个选定的项目.如果我仅使用String来声明所选项目,但如果使用对象则不能,则可以正常工作.

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; }
 }

- 设置所选项目

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

有没有办法解决这个问题?

Is there a way to fix that?

推荐答案

所选项目必须始终是您的项目来源列表之一.您无法创建新对象并将其分配给SelectedItem.组合框仅比较对象引用而不是内容.

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.

这篇关于WPF ComboBox选定的项目参考一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆