在Silverlight中复制超链接内容

时间:2014-11-06 09:11:43

标签: silverlight hyperlink copy mouse selection

如何使用mouse.simialr将Silverlight中的超链接内容复制到文本框中的复制方式。

我想知道如何选择silverlight中的超链接内容,我目前正在使用HyperlinkTextStyle,它周围会出现一个矩形但是当我尝试用鼠标选择超链接中的内容时,我不能。

我使用以下样式:

<Setter.Value>
    <ControlTemplate TargetType="HyperlinkButton">
        <Grid
            Cursor="{TemplateBinding Cursor}"
            Background="{TemplateBinding Background}">
            <vsm:VisualStateManager.VisualStateGroups>
              <vsm:VisualStateGroup x:Name="CommonStates">
                <vsm:VisualState x:Name="Normal"/>
                <vsm:VisualState x:Name="MouseOver">
                  <Storyboard>
                    <ObjectAnimationUsingKeyFrames
                      Duration="0"
                      Storyboard.TargetName="UnderlineTextBlock"
                      Storyboard.TargetProperty="Visibility">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                    </ObjectAnimationUsingKeyFrames>
                  </Storyboard>
                </vsm:VisualState>
                <vsm:VisualState x:Name="Pressed">
                  <Storyboard>
                    <DoubleAnimation Duration="0" To="0.5"
                      Storyboard.TargetProperty="Opacity"
                      Storyboard.TargetName="TextElement"/>
                  </Storyboard>
                </vsm:VisualState>
                <vsm:VisualState x:Name="Disabled">
                  <Storyboard>
                    <ObjectAnimationUsingKeyFrames Duration="0"
                      Storyboard.TargetName="DisabledOverlay"
                      Storyboard.TargetProperty="Visibility">
                      <DiscreteObjectKeyFrame
                        KeyTime="0" Value="Visible"/>
                    </ObjectAnimationUsingKeyFrames>
                  </Storyboard>
                </vsm:VisualState>
              </vsm:VisualStateGroup>
              <vsm:VisualStateGroup x:Name="FocusStates">
                <vsm:VisualState x:Name="Focused">
                  <Storyboard>
                    <DoubleAnimationUsingKeyFrames
                      Storyboard.TargetName="FocusVisualElement"
                      Storyboard.TargetProperty="Opacity">
                      <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                  </Storyboard>
                </vsm:VisualState>
                <vsm:VisualState x:Name="Unfocused"/>
              </vsm:VisualStateGroup>
            </vsm:VisualStateManager.VisualStateGroups>
            <Border Background="{TemplateBinding Background}"
                Margin="{StaticResource PhoneHorizontalMargin}"
                Padding="{TemplateBinding Padding}">
                <TextBlock x:Name="TextElement"
                    HorizontalAlignment="{TemplateBinding
                        HorizontalContentAlignment}"
                    Text="{TemplateBinding Content}"
                    TextDecorations="Underline"
                    VerticalAlignment="{TemplateBinding
                    VerticalContentAlignment}"
                    TextWrapping="Wrap"
                    IsSelectionActive="True"/>
                <TextBlock
                    HorizontalAlignment="{TemplateBinding
                        HorizontalContentAlignment}"
                    Margin="{TemplateBinding Padding}"
                    x:Name="UnderlineTextBlock"
                    VerticalAlignment="{TemplateBinding
                        VerticalContentAlignment}"
                    Visibility="Collapsed"
                    Text="{TemplateBinding Content}"
                    TextDecorations="Underline"
                    Foreground="{TemplateBinding Foreground}"/>
                <TextBlock
                    HorizontalAlignment="{TemplateBinding
                        HorizontalContentAlignment}"
                    Margin="{TemplateBinding Padding}"
                    x:Name="DisabledOverlay"
                    VerticalAlignment="{TemplateBinding
                        VerticalContentAlignment}"
                    Visibility="Collapsed"
                    Canvas.ZIndex="1"
                    Foreground="{StaticResource DisabledColor}"
                    Text="{TemplateBinding Content}"/>
                <ContentPresenter
                    HorizontalAlignment="{TemplateBinding
                        HorizontalContentAlignment}"
                    Margin="{TemplateBinding Padding}"
                    x:Name="contentPresenter"
                    VerticalAlignment="{TemplateBinding
                        VerticalContentAlignment}"
                    Content="{TemplateBinding Content}"
                    ContentTemplate="{TemplateBinding ContentTemplate}"/>
                <Rectangle
                    x:Name="FocusVisualElement"
                    IsHitTestVisible="false"
                    Opacity="0"
                    Stroke="{StaticResource SecondaryBrush}"
                    StrokeThickness="1"/>

2 个答案:

答案 0 :(得分:0)

获取带有可选文本的超链接的最简单方法是使用RichTextBox。试试这个例子:

<RichTextBlock>
    <Paragraph>
        <Span></Span>
        <Hyperlink NavigateUri="http://www.stackoverflow.com">Selectable</Hyperlink>
        <Span></Span>
    </Paragraph>
</RichTextBlock>

以上可能并不完全符合您的要求(您必须单击要选择的文本的左侧或右侧 - 不能在中间单击)。


另一种方法是使用TextBox,将其设置为超链接,并添加鼠标单击事件以执行导航。大致是:

<TextBox x:Name="textBox" 
         Tag="http://www.stackoverflow.com"
         Cursor="Hand" BorderThickness="0" Foreground="Blue"
         Text="Selectable"/>

但这有点棘手,因为您需要弄清楚用户是单击选择还是单击导航。也许最好的方法是处理“鼠标按下”和“鼠标按下”事件,如果它们之间的时间是&lt; x毫秒,然后执行导航。

此外,要使鼠标单击事件起作用,您必须使用AddHandler添加侦听器,因为TextBox本身会在控件内部处理鼠标单击。所以,像这样:

private DateTime? _downClickTime;

public MyPage()
{
    InitializeComponent();

    txt.AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(TextBox_MouseLeftButtonUp), true);
    txt.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(TextBox_MouseLeftButtonDown), true);
}

private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    _downClickTime = DateTime.Now;
}

private void TextBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (_downClickTime.HasValue && DateTime.Now.Subtract(_downClickTime.Value).TotalMilliseconds < 200)
    {
        var textBox = (TextBox)sender;
        string url = textBox.Tag as string;
        HtmlPage.Window.Navigate(new Uri(url));
    }
    _downClickTime = null;
}

答案 1 :(得分:0)

我找到了这种要求的解决方案。希望它可以帮助ohers

                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="UnderlineTextBlock" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="DisabledOverlay" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                        </vsm:VisualStateGroup>
                        <vsm:VisualStateGroup x:Name="FocusStates">
                            <vsm:VisualState x:Name="Focused">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="SelectingText" Storyboard.TargetProperty="Opacity">
                                        <SplineDoubleKeyFrame KeyTime="0" Value="0.25" />
                                    </DoubleAnimationUsingKeyFrames>

                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="Unfocused" >
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="SelectingText" Storyboard.TargetProperty="Opacity">
                                        <SplineDoubleKeyFrame KeyTime="0" Value="0" />
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>

                    <!--<TextBox   Margin="{TemplateBinding Padding}"  Foreground="{TemplateBinding Foreground}"
                              IsReadOnly="True"
                               x:Name="SelectingText" 
                               Text="{TemplateBinding Content}"   Opacity="0"  />-->

                    <TextBox x:Name="SelectingText" Background="Transparent" Foreground="Transparent" FontWeight="{TemplateBinding FontWeight}" 
                             FontStyle="{TemplateBinding FontStyle}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" IsReadOnly="True"
                             Margin="{TemplateBinding Padding}" 
                              Text="{TemplateBinding Content}"  Opacity="0"
                            />

                    <TextBlock HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" x:Name="UnderlineTextBlock" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Visibility="Collapsed" Text="{TemplateBinding Content}" TextDecorations="Underline" Foreground="{TemplateBinding Foreground}" />
                    <TextBlock HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" x:Name="DisabledOverlay" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Visibility="Collapsed" Canvas.ZIndex="1" Foreground="{StaticResource DisabledColor}" Text="{TemplateBinding Content}" />
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" x:Name="contentPresenter" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" />
                    <Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Opacity="0" Stroke="{StaticResource SecondaryBrush}" StrokeThickness="1" />

                </Grid>

将文本框替换为下划线的文本块,并在mousemove中使用