DataContext of parent

97 views Asked by At

I am a newbie WPF. I need to acheive the following: I have a ModelView which contains Observable collection of class "Edata". Edata also contains another ObservableColelction of Class"eParams" which contains 4 properties.

now I have listbox which contains the list of Edata, and another listview which contains the params . Every thing works fine. the challenge is the tool tip. I have in the Edata Class Property called AsStringToolTip. I use this property to give some hint to user and briefed info about the row where the mouse is over.

<ListBox x:Name="lbx1" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding EData}" VerticalAlignment="Center" HorizontalAlignment="Center">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel VirtualizingPanel.VirtualizationMode="Recycling"/>
            </ItemsPanelTemplate>

        </ListBox.ItemsPanel>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Style="{StaticResource Description}" TextWrapping="Wrap">
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0} , {1}">
                            <Binding Path="Edata.category" />
                            <Binding Path="Edata.EId" />

                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>



    <!-- No Compar ListView -->
    <ListView Grid.Column="1" Grid.Row="1" ItemsSource="{Binding SelectedItem.Edata.eparams ,ElementName=lbx1}" Grid.IsSharedSizeScope="True"  >
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel VirtualizingPanel.VirtualizationMode="Recycling"/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.View>
            <GridView  >

                <GridViewColumn Header="Name"  >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate >
                            <StackPanel Orientation="Horizontal" >
                                <TextBlock Text="{Binding Name}">
                                    <TextBlock.ToolTip>
                                        **<TextBlock DataContext="{Binding SelectedValue,ElementName=lbx1}" Text="{Binding Path=AsStringToolTip}">**

                                        </TextBlock>
                                    </TextBlock.ToolTip>

                                    </TextBlock>



                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn Header="ValueString" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" >
                                <TextBlock  Text="{Binding ValueString}" />


                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="value" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock  Text="{Binding value}" />

                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn Header="paramtype">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock  Text="{Binding paramtype}" />


                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView>
        </ListView.View>
    </ListView>

I hope I am clear enough. any Advise. Currently the tool tip shows nothing!!!

1

There are 1 answers

0
mm8 On

Since the Tooltip resides in its own visual tree it cannot find the ListBox when the binding is evaluated.

But you could bind the Tag property of the TextBlock to the ListBox and then bind the element in the Tooltip to the PlacementTarget of the ToolTip itself. It's probably better explained with some sample markup:

<GridViewColumn Header="Name"  >
    <GridViewColumn.CellTemplate>
        <DataTemplate >
            <StackPanel Orientation="Horizontal" >
                <TextBlock Text="Name" Tag="{Binding ElementName=lbx1}">
                    <TextBlock.ToolTip>
                        <ToolTip>
                            <TextBlock Text="{Binding PlacementTarget.Tag.SelectedItem.AsStringToolTip, 
                                                    RelativeSource={RelativeSource AncestorType=ToolTip}}" />
                        </ToolTip>
                    </TextBlock.ToolTip>
                                    </TextBlock>
            </StackPanel>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

The PlacementTarget in the binding path refers to the "Name" TextBlock. The Tag property of this TextBlock returns a reference to the "lbx1" ListBox. You can then get the AsStringToolTip property of the currently selected Edata object.