Silverlight: create a hyperlink button as a text block?

I am trying to make all the words in a text block that have a URI. Here is an approach:

    private static void onTextChanged(DependencyObject dependObj, DependencyPropertyChangedEventArgs e)
    {
        WrapPanel wrapPanel = ((HyperlinkTextBlock)dependObj).LayoutRoot;
        wrapPanel.Children.Clear();

        // TODO: use a real wordbreaker?
        // adding an extra space to the end of the last word. Cry.
        IList<string> words = ((string)e.NewValue).Split(' ').Select(word => word + " ").ToList();
        foreach (string word in words)
        {
            Uri uri;
            if (Uri.TryCreate(word, UriKind.Absolute, out uri))
            {
                // TODO the style is off - the text is too big
                wrapPanel.Children.Add(new HyperlinkButton()
                {
                    Content = word,
                    NavigateUri = uri,
                    TargetName = "_blank",
                    Margin = new Thickness(0),
                    Padding = new Thickness(0),
                });
            }
            else
            {
                wrapPanel.Children.Add(new TextBlock() { Text = word, TextWrapping = TextWrapping.Wrap });
            }
        }
    }

(I would be fully prepared for a more XAML-oriented / declarative way of doing this, but I'm not sure how I would do it.)

This works great (except it would be nice to use real broken text), except that it HyperlinkButtonlooks funny. It is too large and the text will not be wrapped. It seems that he has a bias, I tried to correct setting Marginand Paddingto 0, but it has not solved the problem.

Any other ideas? Actually, I want HyperlinkTextinstead HyperlinkButton, but I don’t think Silverlight 3 for Windows Phone 7 offers this.

+3
3

, , , , ( , ). , , , .

, :

var button = new HyperlinkButton
{
    ...
    Style = (Style) Resources["HyperlinkButtonWithNoIndent"]
};

:

<Style x:Key="HyperlinkButtonWithNoIndent" TargetType="HyperlinkButton">
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="HyperlinkButton">
                <Border Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="0.5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="TextElement"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="TextElement">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                        <TextBlock x:Name="TextElement" Text="{TemplateBinding Content}" TextDecorations="Underline" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
+2

TextBlock, Image StackPanel, . Tap TextBlock . URL-.

TextBlock MyTextBlock = new TextBlock() { Text = "Tap Me!" };
GestureListener listener = GestureService.GetGestureListener( MyTextBlock );
listener.Tap += new EventHandler<GestureEventArgs>( OnMyTextBlockTapped );

:

void OnMyTextBlockTapped( object sender, GestureEventArgs e )
{
  // Navigate to URL
}

, Storyboard , .

+1

blend? , HyperlinkButton.

  • Rclick control

.

0

Source: https://habr.com/ru/post/1782643/


All Articles