Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
330
Control in grid and DataContext issue
posted

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.

 

Parents
  • 54937
    Suggested Answer
    Offline posted

    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.

Reply Children
No Data