2010-12-29 5 views
3

私は、クリック可能なURIであるすべての単語をtextblockで作成しようとしています。Silverlight:ハイパーリンクボタンをテキストブロックのように表示させますか?

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 }); 
      } 
     } 
    } 

を(私は完全にアップし、これを行うのがよりXAML指向/宣言的な方法のためだろうが、私はそのことについて私は行くだろうかわからない:ここで私が撮影したアプローチがあります。)

これは正常に動作します(本当のワードブレーカを使用するために素敵なことだ除いて)、HyperlinkButtonがおかしいことを除いて。大きすぎてテキストが折り返されません。また、私はMarginPaddingを0に設定して修正しようとしましたが、問題は解決していませんでした。

他のアイデアはありますか?実際、HyperlinkButtonの代わりにHyperlinkTextが必要ですが、Windows Phone 7用のSilverlight 3はそれを提供しているとは思いません。

答えて

1

これは決してTextBlockで試したことがありませんが、ImageStackPanelで動作しますので、あなたのケースでも動作するはずです。 TextBlockTapジェスチャーのハンドラーを追加し、それを聞いてください。次に、イベントハンドラで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を開始し、アニメーションが完了したときにナビゲーションを行うことで、タップをアニメーション化できます。

+0

にタップジェスチャーを追加しますテキストブロックも同様に実行可能です –

0

ブレンドでコントロールを再テンプレートしようとしましたか? HyperlinkBut​​tonを構成するすべての構成パーツのプレゼンテーションスタイリングを完全に制御する必要があります。

  1. Rclick制御
  2. テンプレートの編集
  3. 編集コピー
  4. フォーマット離れ

あなたは結果のスタイリングを再利用することができます。

2

ここで私はハイパーリンクボタンのスタイルを抽出し、その後、私は(あなたが述べたように奇数インデントという)を気領域に変更を加えることで思い付いたソリューションです。そうすることで、振る舞いは変更したいこと以外は全く同じです。

あなたは下記のようにもスタイルプロパティを設定したハイパーリンクボタンを作成:

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>