我需要检查BitmapSource是否已经存在于List<BitmapSource>中,但我不确定应该比较什么。将检查该项并将其添加到列表中的方法将从WPF UserControl的路由命令中获取作为参数的UserControl。
我想做这样的事:
if(!selectedImages.Any(x => x.SomeBitmapSourceProperty == e.Parameter.SomeBitmapSourceProperty)
selectedImages.Add(e.Parameter as BitmapSource)我将使用什么来比较两个BitmapSource,并且实际上能够从e.Parameter访问该属性?
发布于 2013-12-24 14:23:06
这个问题并不完全清楚,但您似乎希望使用引用相等(即两个BitmapSource值指向同一个对象)进行检查。你可以用直接的Contains来做这件事
var candidate = (BitmapSource)e.Parameter;
if(!selectedImages.Contains(candidate))
{
selectedImages.Add(candidate);
}发布于 2013-12-24 14:17:44
尝试:
if (selectedImages.Count(x =>
x.SomeBitmapSourceProperty == e.Parameter.SomeBitmapSourceProperty
) == 0) selectedImages.Add(e.Parameter as BitmapSource);https://stackoverflow.com/questions/20762352
复制相似问题