2012-11-20 7 views
5

のみ例:リッチテキスト(FlowDocument?)をビューモデルからRichTextBlockにバインドするにはどうすればよいですか?私はこのようなWinRTの<code>RichTextBlock</code>見にテキストを結合するためにオンラインで見つけることができました

<RichTextBlock> 
    <Paragraph> 
     <Run Text="{Binding Content}"/> 
    </Paragraph> 
</RichTextBlock> 

私は実際にこのようなリッチテキストの外観を示すために見つけることができました唯一の例:私は複数の段落と実行を含むことが私の見解モデルのプロパティにRichTextBlockのテキストをデータバインディングに行くかどう

<RichTextBlock> 
    <Paragraph> 
     <Run>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ligula nisi, vehicula nec eleifend vel, rutrum non dolor. Vestibulum ante ipsum primis in faucibus orci</Run> 
     <Run FontSize="30">luctus</Run> 
     <Run>et ultrices posuere cubilia Curae; Curabitur elementum scelerisque accumsan. In hac habitasse platea dictumst. Maecenas eu nibh vitae nibh laoreet placerat. Duis dolor ante, semper luctus ullamcorper eget, placerat et ligula. Donec placerat tincidunt vehicula. Fusce condimentum lacus quis libero blandit semper sed vel quam. Proin eget nisl lacinia nibh convallis scelerisque at sed massa. Duis commodo tincidunt consequat. Duis malesuada, nisl a pharetra placerat, odio dui suscipit quam, vitae rhoncus sem risus quis odio. Aliquam justo nunc, adipiscing id elementum sit amet, feugiat vel enim. Aliquam pharetra arcu nec elit luctus euismod. Suspendisse potenti.</Run> 
    </Paragraph> 
</RichTextBlock> 

?モデルプロパティを表示するにはどのようなタイプが必要ですか?

私はFlowDocumentを使用することについていくつかの参照を見ましたが、RichTextBlockで動作するかどうかはわかりません。ただし、これらの例であっても、ドキュメントにデータがバインディングされることはありません。

+0

です。 HTML/CSSでのテキスト表示ははるかに簡単です。特に複数の列が必要な場合。 – CoderDennis

答えて

1

私は自分のプロジェクトの1つでRichTextBlockにデータをバインドしました。以下のXAMLを参照してください。私はいくつかの文字列値とイメージを束縛しています。 enter image description here

<common:RichTextColumns x:Name="richTextColumns" Margin="117,0,117,47" VerticalAlignment="Top"> 
<RichTextBlock x:Name="richTextBlock" Width="560" Style="{StaticResource ItemRichTextStyle}" TextWrapping="Wrap"> 
    <Paragraph> 
    <Run FontSize="20" FontWeight="Light" Text="{Binding Title}"/> 
    <LineBreak/> 
    </Paragraph> 
    <Paragraph LineStackingStrategy="MaxHeight"> 
    <InlineUIContainer> 
     <ItemsControl ItemsSource="{Binding Authors}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
      <StackPanel Orientation="Vertical"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
      <StackPanel> 
       <HyperlinkButton Content="{Binding}" 
        VerticalAlignment="Center" FontSize="14" 
        Tapped="AuthorSearchLinkTapped" 
        IsTapEnabled="True" 
        Padding="0, 0, 0, 0"/> 
      </StackPanel> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </InlineUIContainer> 
    </Paragraph> 
    <Paragraph LineStackingStrategy="MaxHeight"> 
    <InlineUIContainer> 
     <Image x:Name="image" MaxHeight="200" Margin="0,20,0,10" Stretch="Uniform" Source="{Binding ImageUrl}"/> 
    </InlineUIContainer> 
    </Paragraph> 
    <Paragraph LineStackingStrategy="MaxHeight"> 
    <InlineUIContainer> 
     <Grid Width="560" Height="125" MaxHeight="125"> 
     <TextBlock Text="Loading ad..." 
       VerticalAlignment="Center" 
       HorizontalAlignment="Center" 
       Style="{StaticResource BasicTextStyle}"/> 
     <UI:AdControl ApplicationId="ec6615c8-dc88-4413-af37-1fc3b5603e85" 
        AdUnitId="104236" 
        Width="250" Height="125" 
        HorizontalAlignment="Center"/> 
     </Grid> 
    </InlineUIContainer> 
    </Paragraph> 
    <Paragraph> 
    <Run FontWeight="SemiLight" Text="{Binding Description}"/> 
    </Paragraph> 
</RichTextBlock> 

<!-- Additional columns are created from this template --> 
<common:RichTextColumns.ColumnTemplate> 
    <DataTemplate> 
    <RichTextBlockOverflow Width="560" Margin="80,0,0,0"> 
     <RichTextBlockOverflow.RenderTransform> 
     <TranslateTransform X="-1" Y="4"/> 
     </RichTextBlockOverflow.RenderTransform> 
    </RichTextBlockOverflow> 
    </DataTemplate> 
</common:RichTextColumns.ColumnTemplate> 
</common:RichTextColumns> 
+0

これは本質的に私の質問の最初のオプションの展開例です。デザイン時にフォーマットを知っていれば、簡単です。しかし、私はこれがどのようにバインドされたテキスト内の任意の大胆な言葉をどのように提供するのか分かりません。 XAMLページにプリセットされていない、データバインディングを使用して豊富な書式設定を行う必要があります。 – CoderDennis

2

RichTextBlockが結合ドキュメントを提供していないようです:あなたは、ページがここにレンダリングされるかのスナップショットを見ることができます。代わりにカスタムRichTextBlockコントロールを使用してそれを達成することができます。試すことができますBindable RichTextBlock

+0

彼のブログ記事で拡張しているRichTextBoxコントロールは、winRT XAMLでは存在しないようです。 – CoderDennis

+0

IMHO、それはRichTextBoxではなくRichTextBlockから継承して動作するはずです。あなたの冒険を教えてください。 – Typist

+0

Windows 8のコントロール 'RichTextBlock'は封印されているので、それを継承することはできません。 – CoderDennis

0

私は同様の状況がありました。私は、いくつかの進行中の操作に基づいて、ステータスメッセージでReadOnly RichTextBoxの内容を更新したかったのです。

<UserControl 
    x:Class="RawhideCsControl.StorageViewUserControl2" 
    <!-- > 

     <RichTextBox x:Name="StatusText" HorizontalAlignment="Left" Height="350.72" VerticalAlignment="Top" Width="636"> 
      <FlowDocument> 
       <Paragraph> 
        <Run Text="{Binding Status}"></Run> 
       </Paragraph> 
      </FlowDocument> 
     </RichTextBox> 

    <!-- > 
</UserControl> 

コード::ここに私がやってしまったものだ

public partial class StorageViewUserControl2 : System.Windows.Controls.UserControl 
    { 
     // . . . 

     StatusChanger statusChanger = new StatusChanger(); 
     private void AddStatusLine(string format, params object[] args) 
     { 
      if (args.Length > 0) 
       format = string.Format(format, args); 

      //m_StorageViewTreeModel.MirrorStatusDisplay += string.Format("{0}\r\n", format); 
      statusChanger.Status += string.Format("{0}\r\n", format); 

     }//end this.AddStatusLine(str) 

     // . . . 
    } // end partial class 


    public class StatusChanger : INotifyPropertyChanged 
    { 
     private string status = string.Empty; 
     public string Status 
     { 
      get { return status; } 
      set 
      { 
       status = value; 
       OnPropertyChanged("Status"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string PropertyName) 
     { 
      if(PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); 
     } 
    } // end class StatusChanger 
+0

これがどのように私の問題を解決するのか分かりません。これにより、ステータスメッセージ内に太字のテキストと通常のテキストが表示されますか?あなたはあなたの答えに豊富な書式を表示しません。これは、文字列へのデータバインディングを示す私の質問に入れたものと同じように見えます。 – CoderDennis

1

を私は同じ問題を抱えていたし、私の解決策は、手動でそれを行うことです。

  1. 更新が発生した場合、あなたのRichTextBlock

を更新し、それがもしそうなら

  • を更新されているあなたの財産であるかどうかを確認し、あなたのview.xaml.cs
  • にnotifyPropertyChangedに登録ここにコードがあります。

    private void Page_OnLoaded(object sender, RoutedEventArgs e) 
         {  
          SetReferences(); 
          (DataContext as INotifyPropertyChanged).PropertyChanged += OnPropertyChanged; 
         } 
    
         private void Page_OnUnloaded(object sender, RoutedEventArgs e) 
         { 
          (DataContext as INotifyPropertyChanged).PropertyChanged -= OnPropertyChanged; 
         } 
    
         private void OnPropertyChanged(object sender, PropertyChangedEventArgs e) 
         { 
          var propertyName = GetPropertyName<MyViewModel, object>(x => x.References); // I use this to avoid bugs at runtime 
          if (e.PropertyName == propertyName) 
           SetReferences(); 
         } 
    
         private void SetReferences() 
         { 
          var references = (DataContext as MyViewModel).References; 
          ReferencesRichTextBlock.Blocks.Clear(); 
          foreach (var reference in references) 
          { 
           var paragraph = new Paragraph(); 
           paragraph.Inlines.Add(new Run { Text = reference.Title, FontWeight = FontWeights.Bold}); 
           paragraph.Inlines.Add(new Run { Text = " : "}); 
           paragraph.Inlines.Add(new Run { Text = reference.Content}); 
           paragraph.Inlines.Add(new LineBreak()); 
    
           ReferencesRichTextBlock.Blocks.Add(paragraph); 
          } 
         } 
    
         public static string GetPropertyName<T, P>(Expression<Func<T, P>> action) where T : class 
         { 
          var expression = (MemberExpression)action.Body; 
          var name = expression.Member.Name; 
    
          return name; 
         } 
    

    そしてもちろん、これは私がこのアプリのためのHTML/CSS/JavaScriptに切り替えてるXAML

    <Page ... 
         Loaded="Page_OnLoaded" 
         Unloaded="Page_OnUnloaded"> 
    ... 
    <RichTextBlock x:Name="ReferencesRichTextBlock"> 
    
              </RichTextBlock> 
    </Page> 
    
  • +0

    これは有望そうです。私は、この質問を投稿したときに取り組んでいたアプリケーションを終了したことはありませんでした。それに戻る予定はありませんでしたが、もう一度やり直しましょう。 – CoderDennis

    関連する問題