来自1个ComboBox中不同表的C#值

时间:2016-11-24 11:00:01

标签: c# visual-studio xaml silverlight

我有一个ComboBox设置如下:

private void SiteChanged(object sender, SelectionChangedEventArgs e)
{
    if (comboBoxSites.SelectedIndex == -1) return;

    Site site = comboBoxSites.SelectedItem.Value as Site;

    comboBoxDetector.Items.Clear();
    if (site != null)
    {
        foreach (Detector detector in site.Detectors)
        {                
                comboBoxDetector.Items.Add(new ComboBoxItem()
                {
                    Content = string.Format("{0} ({1})", detector.Track.TrackName, detector.DetectorID),
                    Tag = detector
                });
        }
    }
    if (comboBoxDetector.Items.Count > 0)
        comboBoxDetector.SelectedIndex = 0;

    btnShow_Click(null, null);
}

现在这向我展示了ComboBox中的正确信息 但是,我想在内容字符串中添加一个额外的东西。

我尝试添加查询作为开始 添加查询后,我的代码如下:

foreach (Detector detector in site.Detectors)
    {
        LoadOperation<DetectorType> loadOp = context.Load(context.GetEnabledDetectorTypesQuery(detector.DetectorID));

        comboBoxDetector.Items.Add(new ComboBoxItem()
        {
            Content = string.Format("{0} ({1})", detector.Track.TrackName, detector.DetectorID),
            Tag = detector
        });
    }

现在,我添加了查询,并没有给出任何错误 但是,我想从查询中获得结果。所以我添加了这段代码:

foreach (Detector detector in site.Detectors)
    {
        LoadOperation<DetectorType> loadOp = context.Load(context.GetEnabledDetectorTypesQuery(detector.DetectorID));
        DetectorType type = loadOp.Entities; //Added this

        comboBoxDetector.Items.Add(new ComboBoxItem()
        {
            Content = string.Format("{0} ({1}) {2}", detector.Track.TrackName, detector.DetectorID, type.Description),
            Tag = detector
        });
    }

现在描述是我想要显示的列。但是,DetectorType type = loadOp.Entities;发出错误:cannot implicitly convert type

有没有办法让我可以向ComboBox显示Description值?

1 个答案:

答案 0 :(得分:0)

loadOp.Entities的类型为IEnumerable<DetectorType>。如果您确定只返回一个实体(或者您只对第一个实体感兴趣),那么您可以写DetectorType type = loadOp.Entities.FirstOrDefault();