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
975
FieldLayout as a resource
posted

I'm using FieldLayouts to determine the layout of a xamDataGrid according to the datasource it's bound to. I started by having the FieldLayouts defined within the datagrid itself, but given I now have quite a few and I want to be able to manipulate them on the fly, I've put then into a resource file and call them in the code behind with the following code:

FieldLayout topLevelLayout = new FieldLayout();
topLevelLayout = (FieldLayout)FindResource("IGFieldLayout_LocationTopLevelItem");
FieldLayout childLayout = new FieldLayout();
childLayout = (FieldLayout)FindResource("IGFieldLayout_LocationChildItem");
xdgItems.FieldLayouts.Clear();
xdgItems.FieldLayouts.Add(topLevelLayout);
xdgItems.FieldLayouts.Add(childLayout);

This works perfectly when I first load the grid, but if I then unload the grid and load it again, I get the error "FieldLayout can not be moved to another DataPresenterBase". Why is this error appearing as resources surely should be able to be used anywhere as long as called correctly? Am I missing something in the way that I'm creating and loading the resource?

  • 69686
    Verified Answer
    posted

     Hello,

    When it comes to resources it gets little complicated. When you use a resource in multiple places, you are using the same object instance. This is called sharing. It is usually what is needed but sometimes not -- like in this case. To use the layouts in the resources as different instances is possible by turning off a property -- x:Shared="False". It is not shown in the designer and is hard to find. For example:

    ... .Resources>

     <igDP:FieldLayout x:Shared="False"  x:Key="IGFieldLayout_LocationTopLevelItem">

    ... .Resources>

    With this property it is possible to add this FieldLayout to two different xamDataGrids at the same time without clearing one of them.

    Hope this helps.

    Alex