2012-10-13 13 views
7

Tôi muốn có thể hiển thị siêu liên kết trong phần tử RichTextBlock, nhưng tôi cũng muốn nó có giao diện đúng. Gần nhất tôi đã có thể để có được là thế này:Làm cách nào để tạo Siêu liên kết trong RichTextBlock không hoàn toàn không được căn chỉnh?

How I was able to get it to look

Các XAML cho điều này được dán bên dưới - về cơ bản, tôi sử dụng InlineUIElement và tôi thay đổi nội dung các tài nguyên để loại bỏ tất cả các crud xung quanh nút. Tui bỏ lỡ điều gì vậy? Cách duy nhất để giải quyết vấn đề này là cuộn liên kết của riêng tôi?

Đây là XAML cho khối văn bản định dạng:

<RichTextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14"> 
    <Paragraph>I just want this 
     <InlineUIContainer> 
      <HyperlinkButton Style="{StaticResource HyperlinkButtonStyle1}">Hyperlink</HyperlinkButton> 
     </InlineUIContainer> 
     to not look like crap 
    </Paragraph> 
</RichTextBlock> 

Và đây là XAML cho tài nguyên nuked:

<Style x:Key="HyperlinkButtonStyle1" TargetType="HyperlinkButton"> 
    <Setter Property="Foreground" Value="{StaticResource HyperlinkForegroundThemeBrush}"/> 
    <Setter Property="Background" Value="{StaticResource HyperlinkButtonBackgroundThemeBrush}"/> 
    <!--<Setter Property="BorderBrush" Value="{StaticResource HyperlinkButtonBorderThemeBrush}"/>--> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="Padding" Value="0"/> 
    <Setter Property="HorizontalAlignment" Value="Left"/> 
    <Setter Property="VerticalAlignment" Value="Bottom"/> 
    <Setter Property="FontFamily" Value="Global User Interface"/> 
    <Setter Property="FontSize" Value="14"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="HyperlinkButton"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="PointerOver"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource HyperlinkPointerOverForegroundThemeBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource HyperlinkPressedForegroundThemeBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource HyperlinkDisabledThemeBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="FocusStates"> 
          <VisualState x:Name="Focused"> 
           <Storyboard> 
            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualWhite"/> 
            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualBlack"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Unfocused"/> 
          <VisualState x:Name="PointerFocused"/> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" VerticalAlignment="Bottom"> 
         <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="0" VerticalAlignment="Bottom"/> 
        </Border> 
        <Rectangle x:Name="FocusVisualWhite" IsHitTestVisible="False" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1"/> 
        <Rectangle x:Name="FocusVisualBlack" IsHitTestVisible="False" Opacity="0" StrokeDashOffset="0.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

Trả lời

6

Các bạn đã thử nhìn vào TextButtonStyle trong StandardStyles.xaml kèm với tiêu chuẩn VS mẫu? Bạn không cần phải tạo siêu liên kết của riêng mình. Trong tàu điện ngầm, bạn hoàn toàn có thể thay thế mẫu kiểm soát, do đó không cần sao chép lại chức năng khi đã có quyền kiểm soát với chức năng bạn muốn.

Chỉnh sửa: Tôi lấy mẫu TextButtonStyle và thay đổi lề thành Margin="3,-20,3,0" và sau đó thêm FontSize="14".

Bây giờ nó trông như thế này:

enter image description here

Lý do nó rất xa là TextBlock mẫu trên TextBlock trong TextButtonStyle mẫu trông như thế này:

<Style x:Key="PageSubheaderTextStyle" TargetType="TextBlock" BasedOn="{StaticResource SubheaderTextStyle}"> 
    <Setter Property="TextWrapping" Value="NoWrap"/> 
    <Setter Property="VerticalAlignment" Value="Bottom"/> 
    <Setter Property="Margin" Value="0,0,0,40"/> 
</Style> 

Thông báo lề. Vì vậy, để sửa chữa nó, chúng tôi hoặc đã phải tạo ra một lợi nhuận tiêu cực trên phụ huynh (như tôi đã làm) hoặc chỉ đơn giản là thay đổi lợi nhuận trên PageSubheaderTextStyle.

+0

Vâng - sử dụng textButtonStyle cho cùng một hiệu ứng chính xác ... –

+0

@ShaharPrish Xem chỉnh sửa của tôi. Bạn chỉ cần chỉnh sửa một chút mẫu để có được ảnh hưởng mà bạn mong muốn – mydogisbox

+0

Cảm ơn bạn. Điều đó dường như làm việc tốt hơn - vẫn không có vẻ tốt, nhưng ít nhất nó được liên kết. Muốn nó "vừa làm việc". –