C
C#3mo ago
remi.nz

✅ Help with Tooltip content binding

I have created a UserControl with dependency Property to accept the string for the TextBlock within my ToolTip XAML:
<Button Style="{DynamicResource TweakInfo}" Visibility="{Binding ToolTipVisibility, ElementName=ucToolTip, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="0,0,10,0">
<iconpack:PackIconBootstrapIcons Kind="ExclamationTriangleFill" Foreground="Orange" Height="20" Width="20"/>

<Button.ToolTip>
<ToolTip Style="{DynamicResource NxToolTip}">
<StackPanel Margin="5">
<TextBlock Style="{DynamicResource ToolTipTitle}" Text="Lorem Ipsum" Margin="5,5,5,0" />
<TextBlock Style="{DynamicResource ToolTipText}" Text="{Binding CaptionText, elementName=ucToolTip}" Margin="5,0,5,5" />
</StackPanel>
</ToolTip>
</Button.ToolTip>
</Button>
<Button Style="{DynamicResource TweakInfo}" Visibility="{Binding ToolTipVisibility, ElementName=ucToolTip, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="0,0,10,0">
<iconpack:PackIconBootstrapIcons Kind="ExclamationTriangleFill" Foreground="Orange" Height="20" Width="20"/>

<Button.ToolTip>
<ToolTip Style="{DynamicResource NxToolTip}">
<StackPanel Margin="5">
<TextBlock Style="{DynamicResource ToolTipTitle}" Text="Lorem Ipsum" Margin="5,5,5,0" />
<TextBlock Style="{DynamicResource ToolTipText}" Text="{Binding CaptionText, elementName=ucToolTip}" Margin="5,0,5,5" />
</StackPanel>
</ToolTip>
</Button.ToolTip>
</Button>
Code-Behind:
public static readonly DependencyProperty ToolTipVisbilityProperty =
DependencyProperty.Register("ToolTipVisbility", typeof(bool), typeof(UCToolTip), new PropertyMetadata(false));

public bool ToolTipVisbility
{
get { return (bool)GetValue(ToolTipVisbilityProperty); }
set { SetValue(ToolTipVisbilityProperty, value); }
}

public static readonly DependencyProperty CaptionTextProperty =
DependencyProperty.Register("CaptionText", typeof(string), typeof(UCToolTip), new PropertyMetadata(string.Empty));
public string CaptionText
{
get { return (string)GetValue(CaptionTextProperty ); }
set { SetValue(CaptionTextProperty , value); }
}
public static readonly DependencyProperty ToolTipVisbilityProperty =
DependencyProperty.Register("ToolTipVisbility", typeof(bool), typeof(UCToolTip), new PropertyMetadata(false));

public bool ToolTipVisbility
{
get { return (bool)GetValue(ToolTipVisbilityProperty); }
set { SetValue(ToolTipVisbilityProperty, value); }
}

public static readonly DependencyProperty CaptionTextProperty =
DependencyProperty.Register("CaptionText", typeof(string), typeof(UCToolTip), new PropertyMetadata(string.Empty));
public string CaptionText
{
get { return (string)GetValue(CaptionTextProperty ); }
set { SetValue(CaptionTextProperty , value); }
}
Adding it to another UserControl:
<local:UCToolTip ToolTipVisibility="true" CaptionText="Hello World" />
<local:UCToolTip ToolTipVisibility="true" CaptionText="Hello World" />
The ToolTipVisibility works just fine but the CaptionText isn't
2 Replies
remi.nz
remi.nzOP3mo ago
I tried using
RelativeSource={RelativeSource self}
RelativeSource={RelativeSource AncestorType=UserControl}
RelativeSource={RelativeSource AncestorType=local:UCToolTip}
RelativeSource={RelativeSource self}
RelativeSource={RelativeSource AncestorType=UserControl}
RelativeSource={RelativeSource AncestorType=local:UCToolTip}
None of them worked It only happens inside the <Button.ToolTip> tags If I use
<Button Style="{DynamicResource TweakInfo}" Tooltip="{Binding CaptionText, ElementName=ucToolTip}" />
<Button Style="{DynamicResource TweakInfo}" Tooltip="{Binding CaptionText, ElementName=ucToolTip}" />
It seems to work just fine but I need my custom Style for it PS: x:Name="ucToolTip" does exists on the very top so idk why is it not finding it properly, secondly I'm not using a UserControl for just a button with tooltip, there are more elements to the UserControl, the tooltip is just a part of it, I removed the other elements from the xaml and code behind so that it's easier for anyone to understand who decides to help Any help would be appreciated, thanks Solved it myself: I binded the CaptionText to the Button Tag and used it from there
Tag="{Binding CaptionText, ElementName=ucToolTip}"

<TextBlock Style="{DynamicResource ToolTipText}" Text="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ToolTip}}" Margin="5,0,5,5" />
Tag="{Binding CaptionText, ElementName=ucToolTip}"

<TextBlock Style="{DynamicResource ToolTipText}" Text="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ToolTip}}" Margin="5,0,5,5" />
!close
Accord
Accord3mo ago
Closed!
Want results from more Discord servers?
Add your server