简体   繁体   中英

Silverlight datagrid column value from another table

I have a specialprice lookup window in my silverlight project which is bound to a specialprice table. I have productcode and price in that table. I want to display the product name for each code in the datagrid. The productnames corresponding to the code are in another table named products. I tried using IValueConverter. Since the Linq query is executed asynchronously, I can't return the value

 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();
   }

any other way I can sort this out?

You need to return the product name as part of the original query.

If you are using Linq to SQL (your question isn't clear on that), in your data source you'll need something like:

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;

This will return the product information through the Product field of your SpecialPrice class as long as you mark the property with the [Include] attribute.

Then in your grid you can simply have a column that binds to the product name like this:

<grid:TextColumn Key="Product.ProductName" ... />

Put your code of async execution in a getter of a new property and then use

       Myvalue="{Binding IsAsync=true, MyViewModelProperty}"

       public string MyViewModelProperty {
            get {
                ////your converter code in synchronous pattern.
            }
       }

So your binding will call the getter asynchronously on it's own and display value accordingly as n when it gets available.

Make sure you don't call the query completed in the getter as that would make it async. The getter has to be synchronous.

如果您使用的是EF + WCF RIA,则可以[Include]该Name属性,并在xaml中直接将其绑定。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM