This is a migrated thread and some comments may be shown as answers.

Inconsistent behavior in GridViewDataColumn template binding

1 Answer 127 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 17 May 2012, 02:47 PM
I have a column in my RadGridView that displays some text and then also contains a button that should only be visible to users with permission to change the data.  I'm using MVVM and the viewmodel exposes a boolean property named UserHasWriteAccess that indicates the user's permission level.  The column and its datatemplate are defined as follows.

<telerik:GridViewDataColumn Header="Reason Code">
    <telerik:GridViewDataColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
 
                <TextBlock Text="Lorem ipsum" />
                <Button Content="Edit"
                        Height="23"
                        HorizontalAlignment="Right"
                        Margin="4,0,0,0"
                        Visibility="{Binding DataContext.UserHasWriteAccess,
                                    Mode=OneTime,
                                    Converter={StaticResource BooleanToVisibilityConverter},
                                    RelativeSource={RelativeSource AncestorType=telerik:RadGridView}}">
            </Grid>
        </DataTemplate>
    </telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>

When UserHasWriteAccess is false, the button's visibility binding works fine most of the time (~99%) and the button does not show.  However, as the user scrolls up and down through the grid, eventually intermittent binding errors will suddenly appear for a row or two for which the binding worked previously.  Sometimes the user has to scroll up and down several times before it happens.  Other times it's immediate.  The errors say:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Telerik.Windows.Controls.RadGridView', AncestorLevel='1''. BindingExpression:Path=DataContext.UserHasWriteAccess; DataItem=null; target element is 'Button' (Name=''); target property is 'Visibility' (type 'Visibility')

Curiously, when I inspect an errored binding using Snoop, it suddenly works again.

Is there a known issue with ancestor binding in RadGridView?

1 Answer, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 18 May 2012, 09:52 AM
Hi Chris,

For performance reasons RadGridView uses virtualization when scrolling . Cells and rows are being reused, recycled, added and removed to/from  the visual tree dynamically. This is why such bindings may occur unreliable. As a workaround I would recommend to expose the object with the relevant property as a static resource on the page. Then use binding to a static resource rather than binding to ancestor. This should do the trick. 

All the best,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Chris
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Share this question
or