[英]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.