Hello,
We dynamically generate controls based on existing controls on our page and place them in the XamDataGrid. Our problem is this, our controls require our DataContext to be set on them and they are instead getting a DataRecord set as their DataContext. The DataRecord's DataItem is only the current item on our CollectionView and we need the DataItem to be our collectionview itself since we bind to properties directly on it. Is there any way around having the DataContext set to a DataRecord? As of right now, we are binding the controls DataContext to the CollectionView itself, but it still gets set to the DataRecord before our binding kicks in and this is unwanted.
Also, some of our styles (same style in and out of grid) setters in them with bindings as their values. These bindings expect the DataContext to be our CollectionView and not a DataRecord. If we left the DataRecord as the DataContext, we can no longer can bind directly to the CollectionView.
Hopefully this makes sense. Does anyone have a solution to this issue?
Thanks.
This is how items type controls work. For example if you bind a ListBox (or any ItemsControl for that matter) to a collection, the itemscontrol will generate a "container" element for each item in the collection. Since those containers need to have the context of what they are representing to know what to show, it sets the DataContext of that container element to the item it represents. If you need to get to what the data context is higher up the element chain you would likely need to using a find ancestor binding (DataContext={Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type Window}}) or something like Josh's DataContextSpy.
Excellent! This DataContextSpy will actually resolve a few of my other issues as well.
Right now, we wrote a binding cloner that will reusing the existing bindings already evaluated source(BindingExpression.DataItem) as the source for the cloned bindings. This currently resolves most of our issues.
That DataContextSpy article helped to clear some things up, so thank you for that.