dotnet / dotnet/wpf

False Binding Failure in Visual Studio 2022

Open
#6,478 5 comments 2 reactions 1 assignee Claimed by @singhashish-wpf View on GitHub
Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

I would like to have a WPF hyperlink style where the underline is the same color as the text (foreground). It can either inherit the value from parent or can be overridden with a local property and both text and underline colors change to the same specified value.

Default styling works (is that because the underline inherits the Pen brush from the Text??), but I need to have my custom style which has different underline thickness. To specify thickness I believe I need to declare its a Pen and its Brush.
I tried to bind the Pen's Brush to the Hyperlink ancestor's Foreground and it works visually, but Visual Studio shows a Binding Failure and I think the "Binding Failure" message is wrong (since the underline colour actually works) and is misleading.

> Cannot find source: RelativeSource FindAncestor, AncestorType='System.Windows.Documents.Hyperlink', AncestorLevel='1'.

Note tried raising against [VS](https://developercommunity.visualstudio.com/t/Possible-misleading-XAML-Binding-Failure/10010829) but got told to raise here

```xml



<Setter Property="Foreground" Value="Red"/>
<Setter Property="TextDecorations">
<Setter.Value>
<TextDecorationCollection>
<TextDecoration Location="Underline">
<TextDecoration.Pen>
<Pen Brush="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Hyperlink}}"
Thickness="5"/>
</TextDecoration.Pen>
</TextDecoration>
</TextDecorationCollection>
</Setter.Value>
</Setter>




Some Dark Gray Text
This hyperlink color comes from Style defined in Window's Resources
This hyperlink color comes from Local foreground override"
Last of text

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.