Search code examples
c#wpfdatacontextdevexpress-wpf

How to find DataContext in Setter Value property?


I try to bind Tooltip to cell value at DevExpress grid control, but I "lost" DataContext at Setter.Value property:

Here the full code of GridControl (only one column):

 <dxg:GridControl Grid.Row="0"                                       
                                 x:Name="grid"
                                 VerticalAlignment="Stretch"                                     
                                 HorizontalAlignment="Stretch"
                                 dx:ThemeManager.ThemeName="Seven"                                     
                                 ScrollViewer.CanContentScroll="True"
                                 ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                 ScrollViewer.VerticalScrollBarVisibility="Auto"                                     
                                 ItemsSource="{Binding ObjectViewModel.Collection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                 SelectedItem="{Binding CurrentElement,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,TargetNullValue=null}"                                                             
                                 >

                                <dxg:GridControl.View>

                                    <!--region #RowCellMenuCustomization-->
                                    <dxg:TableView x:Name="view" AutoWidth="True"
                                                   UseLightweightTemplates="None"
                                                   >

                                        </dxg:TableView.RowCellMenuCustomizations>
                                    </dxg:TableView>
                                    <!--endregion #RowCellMenuCustomization-->
                                </dxg:GridControl.View>

                                <dxg:GridControl.Columns>

                                    <dxg:GridColumn                                            
                            Header="Address"
                            Binding="{Binding Address,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"  
                            AllowEditing="False"                                   
                            HorizontalHeaderContentAlignment="Stretch"
                            Width="*"                                                                                            
                            AllowResizing="True" 
                            HeaderToolTip="Address"                                
                            >
                                        <dxg:GridColumn.CellStyle
                                             >
                                            <Style x:Name="toolTipStyle"
                                                   BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=CellStyle}}"                                                        
                                                   TargetType="dxg:GridCellContentPresenter">                                                                                             
                                                <Setter Property="ToolTip">
                                                    <Setter.Value>
                                                        <TextBlock Text="{Binding Path=Address,RelativeSource={RelativeSource Self},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                                                    </Setter.Value>
                                                </Setter>
                                            </Style>
                                        </dxg:GridColumn.CellStyle>
                                    </dxg:GridColumn>

                                </dxg:GridControl.Columns>                                                                   
                            </dxg:GridControl>

Collection class code:

public class Element
 {
     public String Address 
     {
        return SomeObject.Address;
     }
   // other properties
 }

So, thanks to this answer: it works for some not binding text, but when I try to binding it to property it does not work.

Visual Studio output window log:

System.Windows.Data Error: 40 : BindingExpression path error: 'Address' property not found on 'object' ''TextBlock' (Name='')'. BindingExpression:Path=Address; DataItem='TextBlock' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

It seems, that I lost DataContext, but how to bind it at Setter.Value?

P.S. @Rekshino,I do this and output log is:

System.Windows.Data Error: 40 : BindingExpression path error: 'Address' property not found on 'object' ''EditGridCellData' (HashCode=41748728)'. BindingExpression:Path=DataContext.Address; DataItem='TextBlock' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

Solution

  • Try this:

    <Setter.Value>
        <TextBlock Text="{Binding RowData.Row.Address}"/>
    </Setter.Value>
    

    Or:

    <dxg:GridColumn.CellStyle>
        <Style x:Name="toolTipStyle"
                       BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=CellStyle}}"                                                        
                       TargetType="dxg:GridCellContentPresenter">
            <Setter Property="ToolTip" Value="{Binding RowData.Row.Address}"/>
        </Style>
    </dxg:GridColumn.CellStyle>