【发布时间】: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