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

How to override GridViewCell's context

1 Answer 51 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Geoffrey
Top achievements
Rank 1
Geoffrey asked on 20 Jun 2013, 10:48 PM
I've seen in several forum posts (like here: http://www.telerik.com/community/forums/wpf/gridview/binding-to-cell-of-gridview-and-datacontext-says-datarow.aspx) that the Data Context for a cell is the entire object, not just the bound property. Is there a way to change this behavior so the data context is only what the column is bound to? We are binding to complex objects that are all the same type (called ValueWithFormat) and would like to set cell properties based on this type. Here's my example cell template.
<DataTemplate x:Key="ValueWithFormatCellTemplate">
        <Grid Background="{Binding BackgroundBrush}">
            <Border BorderBrush="{Binding BorderBrush}" BorderThickness="1">
                <TextBlock FontWeight="{Binding FontWeight}"
                           Foreground="{Binding ForegroundColor}"
                           Text="{Binding}"
                           ToolTip="{Binding ToolTip}" />
            </Border>
        </Grid>
    </DataTemplate>

1 Answer, 1 is accepted

Sort by
0
Geoffrey
Top achievements
Rank 1
answered on 21 Jun 2013, 06:50 PM
I was able to solve this issue with the information from this post http://www.telerik.com/community/forums/wpf/gridview/gridviewdatacolumn-celltemplate-datatemplate-triggers.aspx 

The modified template looks like this
<DataTemplate x:Key="ValueWithFormatCellTemplate">
        <Grid Background="{Binding BackgroundBrush}"
DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:GridViewCell}},Path=Value}">
            <Border BorderBrush="{Binding BorderBrush}" BorderThickness="1">
                <TextBlock FontWeight="{Binding FontWeight}" Foreground="{Binding ForegroundColor}"
                           Text="{Binding}"
                           ToolTip="{Binding ToolTip}" />
            </Border>
        </Grid>
    </DataTemplate>
Tags
GridView
Asked by
Geoffrey
Top achievements
Rank 1
Answers by
Geoffrey
Top achievements
Rank 1
Share this question
or