The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
HyperlinkButton Attributes - Set Underline as Style RRS feed

  • Question

  • I am working with C# and Xaml for a Windows Phone 8.1 app project, and setting attributes to a HyperlinkButton in C#;

    The below code sample is OK, except for the line: Style = newUL

    "Cannot implicitly convert type 'Windows.UI.Xaml.Documents.Underline' to 'Windows.UI.Xaml.Style'

    Any insight, guidance is appreciated;

    private HyperlinkButton GetAsLink(string tweetWord)
            {
                SolidColorBrush solidColorBrush = new SolidColorBrush(Colors.White);
                SolidColorBrush solidColorBrushBG = new SolidColorBrush(Colors.Blue);
    
                double testFont = new double();
    
                testFont = 22;
    
                Underline newUL = new Underline();            
    
                var hl = new HyperlinkButton
                {
                    NavigateUri = new Uri(tweetWord),
                    Foreground = solidColorBrush,
                    Content = tweetWord,
                    FontSize = testFont,
                    FontWeight = FontWeights.Bold,
                    Background = solidColorBrushBG,
                    Style = newUL
                };
    
                return hl;
            }

     


    robert hellestrae

    Wednesday, October 8, 2014 8:02 PM

Answers

  • Style is essentially a collection of property settings to describe the Control. It is not related to text styling or the Document.Underline class. The Underline class can only be used within a Document type such as a TextBlock or a RichTextBlock.

    The HyperlinkButton can take any content, so you can set its content to a TextBlock using the Underline tag:

            <HyperlinkButton>
                <TextBlock>
                    <Underline>Lorem ipsum</Underline>
                </TextBlock>
            </HyperlinkButton>
    Adding this to your C# code (extra properties removed for simplicity)
                TextBlock tb = new TextBlock();
                Underline ul = new Underline();
                ul.Inlines.Add(new Run() { Text = tweetWord });
                tb.Inlines.Add(ul);
                
                var hl = new HyperlinkButton
                {
                    NavigateUri = new Uri(tweetWord),
                    Content = tb,
                };


    • Marked as answer by rrobhelle Thursday, October 9, 2014 2:03 AM
    Thursday, October 9, 2014 12:09 AM