开发者

How to get the TextBlock Text property in this snippet?

<TextBlock Text="Hi I am Parent Tooltip" Grid.Row="2" >
        <TextBlock.ToolTip>
            <ToolTip >
                <!--<TextBlock x:Name="test" Text="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type TextBlock},AncestorLevel=2},Path=Name}"> </TextBlock>-->
                <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBlock},AncestorLevel开发者_如何转开发=2},Path=Text}"></TextBlock>
            </ToolTip>
        </TextBlock.ToolTip>
    </TextBlock>


        <TextBlock Text="Hi I am Parent Tooltip" Grid.Row="2" >
        <TextBlock.ToolTip>
            <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
                 <TextBlock TextWrapping="Wrap"  Text="{Binding Text}"/>  <!-- tooltip content -->
            </ToolTip>
        </TextBlock.ToolTip> 
    </TextBlock>


<TextBlock x:Name="textBlock" Text="Hi I am Parent Tooltip" Grid.Row="2" >
        <TextBlock.ToolTip>
            <ToolTip >
                <TextBlock x:Name="toolTip" Text="{Binding Path=Text, ElementName=textBlock}"></TextBlock>
            </ToolTip>
        </TextBlock.ToolTip>
    </TextBlock>

Edit: Added a name to the tooltip TextBlock above. Then add the following code to your code behind (in the constructor):

NameScope.SetNameScope(toolTip, NameScope.GetNameScope(this));

That should work.


you can name your textbox and bind using ElementName

you can also have a datacontext set to an object with this text as a property (mvvm is your friend?)


This may be new to .Net 4.0, but this works for me:

<TextBox Text="{Binding SelectedHour}" ToolTip="{Binding Path=Text, RelativeSource={x:Static RelativeSource.Self}}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜