My grid is bound to a collection of SrvItem objects, unfortunately, I need to also display some ancillary data in each row the requires another webservice lookup using one of the fields of the SrvItem object. At first, I thought I'd use an Unbound column and a ValueConverter, but that won't work due to the the asynchronous nature of WCF Data Service calls. Basically I;d need to do something like this to get the data I need:
DataServiceQuery<CIRCUIT> query = (DataServiceQuery<CIRCUIT>)from c in MainPage.context.CIRCUITs.Expand("EQUIPMENTs") where c.PCIRCUITGROUP_Id == item.CCIRCUITGROUP_Id select c;
query.BeginExecute(result => App.Current.RootVisual.Dispatcher.BeginInvoke(() =>
{
data = query.EndExecute(result).ToList(); // get the data
// then massage the data and display it in the unbound or custom column
}), null);
Since a ValueConverter won't work, could I perhaps create a custom column type and somehow bind it to item.CCIRCUITGROUP_Id but make it display a custom value based on the results of the async query?
My data object is all generated from the DB via WCF Data Service and the value I need to display is related to some other data in the model but really doesn't have an property of its own in the displayable object so properties are a no go, but creating a custom column type to make the service call and display the data worked great. Thanks for you help. FWIW, below is the code for my custom column's content provider that does the heavy lifting:
public class EquipmentColumnContentProvider : ColumnContentProviderBase
TextBlock _tb;
static Dictionary<int, string> _cache = new Dictionary<int, string>();
public EquipmentColumnContentProvider()
_tb = new TextBlock();
}
public override FrameworkElement ResolveDisplayElement(Cell cell, Binding cellBinding)
return _tb;
void circuits_LoadCompleted(object sender, LoadCompletedEventArgs e)
DataServiceCollection<CIRCUIT> circuits = sender as DataServiceCollection<CIRCUIT>;
if (e.Error == null)
if (circuits.Continuation != null)
circuits.LoadNextPartialSetAsync();
else
List<string> items = new List<string>();
foreach (CIRCUIT c in circuits)
foreach (EQUIPMENT eq in c.EQUIPMENTs)
items.Add(eq.LCE);
this._tb.Text = string.Join(",", items.Distinct().ToArray());
if (circuits.Count > 0)
int groupid = circuits[0].PCIRCUITGROUP_Id.Value;
if (_cache.ContainsKey(groupid))
_cache[groupid] = _tb.Text;
_cache.Add(groupid, _tb.Text);
circuits.LoadCompleted -= new EventHandler<LoadCompletedEventArgs>(circuits_LoadCompleted);
public override void AdjustDisplayElement(Cell cell)
SrvService ss = cell.Row.Data as SrvService; SrvItem item = ss.SrvItems.FirstOrDefault(o => o.ParentItemID == null && o.PrdComponent.Component != "Directory");
string value; CIRCUIT child = null;
_tb.Text = "";
if (item != null && item.CIRCUITs1.Count > 0)
child = item.CIRCUITs1[0]; // MG messed up the relation in their schema - it should be one for one but is one to many so just take the first one
if (item != null && child.CCIRCUITGROUP_Id != null && _cache.TryGetValue(child.CCIRCUITGROUP_Id.Value, out value))
_tb.Text = value;
else if (child.CCIRCUITGROUP_Id != null)
if (!_cache.ContainsKey(child.CCIRCUITGROUP_Id.Value))
_cache.Add(child.CCIRCUITGROUP_Id.Value, "");
OmniaEntities ctx = new OmniaEntities(new Uri("Omnia.svc", UriKind.Relative));
DataServiceCollection<CIRCUIT> circuits = new DataServiceCollection<CIRCUIT>();
circuits.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(circuits_LoadCompleted);
circuits.LoadAsync(from c in ctx.CIRCUITs.Expand("EQUIPMENTs") where c.PCIRCUITGROUP_Id == child.CCIRCUITGROUP_Id select c);
protected override FrameworkElement ResolveEditorControl(Cell cell, object editorValue, double availableWidth, double availableHeight, Binding editorBinding)
return null;
public override object ResolveValueFromEditor(Cell cell)
Hi,
Sorry for the delayed response.
You could potentially create a CustomColumn, but the best way to really handle this is to have a property on your data object, that you can update with the data you've gotten back from your request. When you set that property, you can raise the PropertyChanged event for the object, and the cell for that column would automatically be updated.
Is that scenario feasible with your data?
Also, just as an FYI on creating custom columns, a colleague of mine posted a nice tutorial:
http://blogs.infragistics.com/blogs/devin_rader/archive/2010/07/08/creating-custom-columns-for-xamgrid.aspx
-SteveZ