如何在组合框中显示子对象的名称?

时间:2019-01-17 10:28:45

标签: wpf oop mvvm combobox

我有一个复杂的Article对象,其中列出了ArticleLocation类型的Locations。我需要在GridView内的组合框中显示这些位置:

public class Article : INotifyPropertyChanged
{
    private int sapNumber;
    private string descript;
    private ObservableCollection<ArticleLocation> locations;
    private ArticleLocation selectedLocation;


    public int SAPNumber
    {
        get => sapNumber;
        set
        {
            if (sapNumber != value)
            {
                sapNumber = value;
                RaisePropertyChanged("SAPNumber");
            }
        }
    }

    public string Description
    {
        get => descript;
        set
        {
            if (descript == null || !descript.Equals(value))
            {
                descript = value;
                RaisePropertyChanged("Description");
            }
        }
    }

    internal ObservableCollection<ArticleLocation> Locations { get => locations; set => locations = value; }
    internal ArticleLocation SelectedLocation { get => selectedLocation; set => selectedLocation = value; }

}

我需要显示这样存储的位置:

class ArticleLocation : INotifyPropertyChanged
{
    private string location;
    private double available;

    public string Location { get => location; set => location = value; }
    public double Available { get => available; set => available = value; }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

我现在拥有的组合框:

<telerik:RadComboBox ItemsSource="{Binding Locations}" DisplayMemberPath="Location" SelectedItem="{Binding SelectedLocation}" SelectionChanged="RadComboBox_SelectionChanged"/>

我不知道如何获取要显示的位置,以便可以选择它。我唯一想到的替代方法是将位置名称和可用项保存在单独的列表中...

并不是应该有很大的不同,但是我正在使用wpf格式的telerik对象。

2 个答案:

答案 0 :(得分:0)

文章类别:

public class Article : INotifyPropertyChanged
{
    public ObservableCollection<ArticleLocation> locations;
    public string Location { 
        get => location; 
        set
        {
            if (location == null || !location.Equals(value))
            {
                 location = value;
                 RaisePropertyChanged("Location");
            }
        }
    }
}

ArticleLocation类:

public class ArticleLocation : INotifyPropertyChanged
{
    private string location;

    public string Location { 
        get => location; 
        set
        {
            if (location == null || !location.Equals(value))
            {
                location = value;
                RaisePropertyChanged("Location");
            }
        }
    }
}

在您的xaml.cs类中:

public MainWindow()
{
    InitializeComponent();
    Article article = new Article();
    this.DataContext = article;
}

答案 1 :(得分:0)

我发现了问题,由于未将ArticleLocation类公开,因此Locations和SelectedLocation属性被创建为内部属性,因此XAML视图无法对其进行访问。

将ArticleLocation类更改为公共,并将我的Article类属性更改为公共,它们开始显示在comobox中。