我有一个wpf添加树视图项目到列表框如下:
private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
var itemToAdd = this.tv_Project.SelectedItem as Product;
if(itemToAdd != null)
{
lb_Configuration.Items.Add(itemToAdd);
}
else
{
...
}
}
与ItemsSource绑定的列表框如:
<ListBox x:Name="lb_Configuration" SelectionMode="Single" ItemsSource="{Binding Products}" SelectedItem="{Binding SelectedProduct}">
上面的代码工作正常。现在,当我检测到RegisterLowByte
与ModelNumber
具有相同Products
时,我想自动将相应的Product
添加到列表框中,所以我尝试了这个:
private void FindDevice_Executed(object sender, ExecutedRoutedEventArgs e)
{
var RegisterLowByte = BitConverter.GetBytes(infoRegisters[j])[0];
var hasItem = sdb.GetProductFilteredWithLinq(RegisterLowByte) as Product;
if (hasItem != null)
{
lb_Configuration.Items.Add(hasItem);
}
else
{
...
}
}
获得产品的方法:
public ICollection<Product> GetProductFilteredWithLinq(UInt16 modelNumber)
{
ICollection<Product> products = GetProducts();
IEnumerable<Product> matches = from product in products
where product.ModelNumber == modelNumber
select product;
return new ObservableCollection<Product>(matches.ToList());
}
尝试此操作后,hasItem
始终为空,但如果我尝试定义hasItem
,则
var hasItem = sdb.GetProductFilteredWithLinq(RegisterLow).Cast<Product>();
我可以获得与Product
具有相同ModelNumber
的{{1}},但我无法将其作为RegisterLowByte
添加到列表框中,任何人都知道如何修复此问题?提前谢谢!