[英]Silverlight datagrid column value from another table
我的Silverlight项目中有一个特惠价格查找窗口,该窗口绑定到特惠价格表。 我在那张桌子上有产品代码和价格。 我想为数据网格中的每个代码显示产品名称。 与代码相对应的产品名称在另一个名为产品的表中。 我尝试使用IValueConverter。 由于Linq查询是异步执行的,因此我无法返回该值
string ProductName = "";
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string ProdCode = value.ToString();
var GetProdNamesQuery = GV.dbContext.Load(GV.dbContext.GetProdNamesQuery(ProdCode));
GetProdNamesQuery.Completed += new EventHandler(GetProdNamesQuery_Completed);
return ProductName;
}
void GetProdNamesQuery_Completed(object sender, EventArgs e)
{
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
还有其他方法可以解决吗?
您需要返回产品名称作为原始查询的一部分。
如果您正在使用Linq to SQL(您的问题尚不清楚),则在数据源中您将需要以下内容:
DataLoadOptions options = new DataLoadOptions();
options.LoadWith<SpecialPrice>(p => p.Product);
this.DataContext.LoadOptions = options;
var query = from specialPrice in DataContext.SpecialPrices
select party;
return query;
只要您使用[Include]
属性标记该属性,这将通过SpecialPrice
类的Product
字段返回产品信息。
然后,在网格中,您可以简单地将一列绑定到产品名称,如下所示:
<grid:TextColumn Key="Product.ProductName" ... />
将您的异步执行代码放入新属性的getter中,然后使用
Myvalue="{Binding IsAsync=true, MyViewModelProperty}"
public string MyViewModelProperty {
get {
////your converter code in synchronous pattern.
}
}
因此,绑定将独自异步调用getter,并在可用时将其相应地显示为n。
确保您没有在getter中调用完成的查询,因为这会使查询异步。 吸气剂必须是同步的。
如果您使用的是EF + WCF RIA,则可以[Include]
该Name属性,并在xaml中直接将其绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.