How can I make FirstBlock as DependencyProperty?

Answered How can I make FirstBlock as DependencyProperty?

  • 21 Agustus 2012 7:26
     
      Memiliki Kode

    Hi all,

    I have to write a text in the first paragraph of RichtextBox to a TextBlock.

    I  tried this:

    <TextBlock Text= {Binding ElementName=rtb, Path=Docoment.Blocks[0]} />

    This will  not work since FirstBlock is not a dependency property.

    How can I do this?

    Regards

    Agrawal



Semua Balasan

  • 21 Agustus 2012 9:44
     
     

    Hi,

    Add in your binding this text UpdateSourceTrigger="PropertyChanged"  after setting this property , the changes made in source will be reflected in Textblock ,

    here is the link for that

    http://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger.aspx

    and the Binding class

    http://msdn.microsoft.com/en-us/library/system.windows.data.binding.aspx


    Thanks & Regards dhampall

  • 21 Agustus 2012 10:02
     
     

    HI dhampall_79(Partner)

    Thanks for your replay.

    It looks like FirstBlock is not a dependancy property.

    So adding any of these attributes will not make any difference .

    In order to work like this wi shold make fst block as dependencyproperty.

    How can I do that?

    Regards

    Agrawal

  • 21 Agustus 2012 10:26
     
      Memiliki Kode

    Hi Agarwak,

    Try This works fine

    <TextBlock x:Name="text1" Grid.Row="0" Text="{Binding ElementName=Para1, Path=Inlines/Text}"></TextBlock>
                <RichTextBox x:Name="RichText" Grid.Row="1">
                    <FlowDocument>
                        <Paragraph x:Name="Para1">
                            <Run> 1st Paragraph </Run>
                        </Paragraph>
                        <Paragraph x:Name="Para2">
                            <Run>2st Paragraph</Run>
                        </Paragraph>
                        <Paragraph x:Name="Para3">
                            <Run>3st Paragraph</Run>
                        </Paragraph>
                        <Paragraph x:Name="Para4">
                            <Run>4st Paragraph</Run>
                        </Paragraph>
                    </FlowDocument>
                </RichTextBox>

    Happy Programming!!!

    • Ditandai sebagai Jawaban oleh agrawal.ashish 21 Agustus 2012 10:48
    • Tanda sebagai Jawaban dihapus oleh agrawal.ashish 21 Agustus 2012 10:49
    •  
  • 21 Agustus 2012 10:48
     
     

    Hi Srithar 

    It will work only for first time not when you change the paragraph any further.

    Regards

    Agrawal


  • 21 Agustus 2012 19:27
     
      Memiliki Kode

    Hi Agarwal,

    This works in all cases

    <TextBlock x:Name="text1" Grid.Row="0"

    Text="{Binding Path=Document.Blocks.FirstBlock.Inlines/Text, ElementName=RichText}"></TextBlock> <RichTextBox Grid.Row="1" x:Name="RichText"> <FlowDocument> <Paragraph> <Run>1 Paragraph</Run> </Paragraph> <Paragraph> <Run>2 Paragraph</Run> </Paragraph> <Paragraph> <Run>3 Paragraph</Run> </Paragraph> <Paragraph> <Run>4 Paragraph</Run> </Paragraph> <Paragraph> <Run>5 Paragraph</Run> </Paragraph> </FlowDocument> </RichTextBox>

    Happy Programming!!!
  • 22 Agustus 2012 3:51
     
     

    Hi Srithar,

     I copy pasted your last post code but still it's not working

     it's only working for first time if we change the First Block content at run time let say

     "1 Paragraph" to "new paragraph" but the textblock show "1 Paragraph" in place of "new  Paragraph"

    Regards

    Ashish

  • 22 Agustus 2012 4:39
     
      Memiliki Kode

    Hi ashish,

    I works fine for me. Even when i change it at runtime it is updated.

    Code Behind C#:

    RichText.Document.Blocks.Clear();
                RichText.Document.Blocks.Add(new Paragraph(new Run("Changed the First Paragraph")));
                RichText.Document.Blocks.Add(new Paragraph(new Run("WPF is Great")));
                RichText.Document.Blocks.Add(new Paragraph(new Run("How is your day")));
                RichText.Document.Blocks.Add(new Paragraph(new Run("4 Paragraph")));
                RichText.Document.Blocks.Add(new Paragraph(new Run("Last Paragraph")));

    Output:

  • 22 Agustus 2012 5:28
     
     Jawab Memiliki Kode

    Not sure off the top of my head how best to display the Inlines in the TextBlock, but this'll make FirstBlock bindable, and there's a method in there to pull out the text of FirstBlock.

    <StackPanel>
        <WpfApplication300:RichTextBoxEx x:Name="rtb"></WpfApplication300:RichTextBoxEx>
        <TextBlock Text="{Binding ElementName=rtb,Path=FirstBlock,Mode=OneWay}"></TextBlock>
        <TextBlock Text="{Binding ElementName=rtb,Path=FirstBlockText,Mode=OneWay}"></TextBlock>
    </StackPanel>

    public class RichTextBoxEx : RichTextBox, INotifyPropertyChanged
    {
        public RichTextBoxEx()
        {
            this.TextChanged += new TextChangedEventHandler(RichTextBoxEx_TextChanged); // make sure we know when the text changes
        }
    
        void RichTextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
        {
            FirstBlock = this.Document.Blocks.FirstBlock; // update our dependency property
        }
    
        public static string GetBlockText(Block block) // get the text from the block
        {                        
            if (block == null) return null;
    
            TextRange textRange = new TextRange(block.ContentStart, block.ContentEnd);
    
            return textRange.Text;
        }
    
        public Block FirstBlock
        {
            get { return (Block) GetValue(FirstBlockProperty); }
            set
            {
                SetValue(FirstBlockProperty, value);
    
                OnPropertyChanged("FirstBlockText"); // when we set a new first block, update the FirstBlockText property
            }
        }
    
        public string FirstBlockText
        {
            get { return GetBlockText(FirstBlock); }            
        }
    
        // Using a DependencyProperty as the backing store for FirstBlock.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FirstBlockProperty =
            DependencyProperty.Register("FirstBlock", typeof(Block), typeof(RichTextBox));
    
        #region Implementation of INotifyPropertyChanged
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    
        #endregion
    }

    Warm regards,

    Matt