简体   繁体   English

来自另一个表的Silverlight DataGrid列值

[英]Silverlight datagrid column value from another table

I have a specialprice lookup window in my silverlight project which is bound to a specialprice table. 我的Silverlight项目中有一个特惠价格查找窗口,该窗口绑定到特惠价格表。 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. 我尝试使用IValueConverter。 Since the Linq query is executed asynchronously, I can't return the value 由于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();
   }

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: 如果您正在使用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;

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. 只要您使用[Include]属性标记该属性,这将通过SpecialPrice类的Product字段返回产品信息。

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 将您的异步执行代码放入新属性的getter中,然后使用

       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. 因此,绑定将独自异步调用getter,并在可用时将其相应地显示为n。

Make sure you don't call the query completed in the getter as that would make it async. 确保您没有在getter中调用完成的查询,因为这会使查询异步。 The getter has to be synchronous. 吸气剂必须是同步的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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