I’m trying to pass a TextBlock to a converter through binding
<DataTemplate>
<Grid>
<DockPanel LastChildFill="False" x:Name="dpInfo">
<Image Source = "{Binding Data.Picture}" Height="42" HorizontalAlignment="Left" Stretch="Uniform" VerticalAlignment="Center" Width="32" Margin="16,0,0,0" DockPanel.Dock="Left"/>
<TextBlock Text= "{Binding Data.Name}" x:Name="tbUserName" VerticalAlignment="Center" Padding="10,0,0,0" TextTrimming="CharacterEllipsis" DockPanel.Dock="Left"/>
<TextBlock Text= "{Binding Data.ScribbleCount, StringFormat=' ({0})', Mode=TwoWay}" VerticalAlignment="Center" DockPanel.Dock="Left"/>
</DockPanel>
<Rectangle x:Name="rctClickableArea" MouseLeftButtonDown="User_MouseLeftButtonDown" Fill="Black" Opacity="0">
<Rectangle.ToolTip>
<ToolTip DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={x:Static RelativeSource.Self}}">
<ToolTip.Visibility>
<MultiBinding Converter="{StaticResource TooltipVisibilityConverter}">
<MultiBinding.Bindings>
<Binding Path="Data.Name"/>
<Binding ElementName="tbUserName"/>
</MultiBinding.Bindings>
</MultiBinding>
</ToolTip.Visibility>
<TextBlock Text="{Binding Data.Name}"/>
</ToolTip>
</Rectangle.ToolTip>
</Rectangle>
</Grid>
</DataTemplate>
The binding to the DataContext (Binding Path=”Data.Name”) works but the binding (Binding ElementName=”tbUserName”) does not work, where tbUserName is a TextBlock in the dbInfo DockPanel. Do you have any ideas on how to bind this correctly?
You might be having two issues…
NameScoping …
ElementName binding has stringent restrictions about NameScoping…
So in your case referring element outside the ToolTip’s context fails. Now for this to work correct you will have to bridge the broken scope using something like
Tagproperty…Visibility of ToolTip
Instead of binding to visibility of tooltip, you could use a data-trigger with multi binding on rectangle itself and set the tooltip that you want, based on the condition value…
Notice that
TooltipVisibilityConverternow returns a boolean for sake of simplicity.