Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 1.56 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. passing a TextBlock or collection of inlines to a child UserControl in WPF
  2. <myNS:InfoBox Text="Some Text"/>
  3.        
  4. public static readonly DependencyProperty TextProperty =
  5.      DependencyProperty.Register("Text", typeof(string), typeof(InfoBox),
  6.      new UIPropertyMetadata(null,ValueChanged));
  7.        
  8. private static void ValueChanged(DependencyObject dpo,
  9.                                      DependencyPropertyChangedEventArgs args)
  10.     {
  11.         ((InfoBox)dpo).TextBlock.Text = (string)args.NewValue;
  12.     }
  13.        
  14. <myNS:InfoBox>
  15.         <myNS:InfoBox.ReferenceBlock>
  16.              <TextBlock>
  17.                 <Run Language="en-gb" Text="SampleSample"/><LineBreak/>
  18.                 <Run Language="en-gb"/><LineBreak/>
  19.                 <Run Language="en-gb" Text="MoreMoreMore"/>
  20.              </TextBlock>  
  21.         <myNS:InfoBox.ReferenceBlock>          
  22. </myNS:InfoBox>
  23.        
  24. public static readonly DependencyProperty ReferenceBlockProperty =
  25.         DependencyProperty.Register("ReferenceBlock", typeof(TextBlock),
  26.         typeof(InfoBox), new UIPropertyMetadata(null, ReferenceBlockReceived));
  27.  
  28. [...]
  29.  
  30.  
  31.      private static void ReferenceBlockReceived(DependencyObject dpo,
  32.             DependencyPropertyChangedEventArgs args)
  33.     {
  34.         var textblock = (TextBlock)args.NewValue;
  35.         if (textblock != null)
  36.         {
  37.             ((InfoBox)dpo).TextBlock.Inlines.Clear();
  38.             ((InfoBox)dpo).TextBlock.Inlines.AddRange(textblock.Inlines);
  39.         }
  40.     }
  41.        
  42. <ControlTemplate TargetType="myNS:InfoBox">
  43.     <ContentControl Content="{TemplateBinding ReferenceBlock}" />
  44. </ControlTemplate>