【问题标题】:C# values from different table in 1 ComboBox来自 1 个 ComboBox 中不同表的 C# 值
【发布时间】:2017-04-08 15:43:45
【问题描述】:

我有一个 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 中的正确信息。
但是,我想在内容字符串中添加 1 个额外的东西。

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

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

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

【问题讨论】:

  • 这个错误清楚地表明您不能将 loadOp.Entities 的类型隐式转换为 DetectorType。如果您绝对确定它是同一类型,那么您可以使用 DetectorType type = (DetectorType)loadOp.Entities; 显式转换类型
  • @m.rogalski 我 100% 确定,因为当我将它放在单独的方法中并调用它时,它会向我显示结果。将其更改为您告诉的内容也会给我同样的错误。

标签: c# visual-studio xaml silverlight


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多