none
FlowDocument.FindName

    Question

  • Hi, I have a FlowDocument built by code with names identifying some of its Span's; anyway, I cannot manage to have FindName method find the named spans. E.g. say I have this code:

    FlowDocument doc = new FlowDocument();
    Paragraph par = new Paragraph();
    Span span;
    for (int i = 1; i <= 10; i++)
    {
        span = new Span(new Run(i.ToString()));
        span.Name = "x" + i.ToString(CultureInfo.InvariantCulture);
        par.Inlines.Add(span);
    }
    doc.Blocks.Add(par);
    span = doc.FindName("x3") as Span;   // rets NULL!!

    How is it that this does not work?

    Saturday, March 29, 2008 12:03 PM

Answers

  • Hi Naftis,

     

    you must register your elements in a Namescope by using the RegisterName-Method. After that they are accessible via FindName. If you define your elements in XAML, this is automatically done for you.

    Code Snippet

     

    FlowDocument doc = new FlowDocument();

    NameScope.SetNameScope(doc, new NameScope());

    Paragraph par = new Paragraph();

    Span span;

    for (int i = 1; i <= 10; i++)

    {

    span = new Span(new Run(i.ToString()));

    span.Name = "x" + i.ToString(CultureInfo.InvariantCulture);

    par.Inlines.Add(span);

    doc.RegisterName(span.Name, span);

    }

    doc.Blocks.Add(par);

    span = doc.FindName("x3") as Span; // rets No more NULL!!

     

     

     

    An alternative is to use the FindLocigalNode-Method of LogicalTreeHelper-class. Internally this method searches from the specified element done the logical tree. the first element with the specified name is returned.

     

    Code Snippet

    FlowDocument doc = new FlowDocument();

    Paragraph par = new Paragraph();

    Span span;

    for (int i = 1; i <= 10; i++)

    {

    span = new Span(new Run(i.ToString()));

    span.Name = "x" + i.ToString(CultureInfo.InvariantCulture);

    par.Inlines.Add(span);

    }

    doc.Blocks.Add(par);

    span = LogicalTreeHelper.FindLogicalNode(doc,"x3") as Span;

     

     

    Saturday, March 29, 2008 2:35 PM

All replies

  • Hi Naftis,

     

    you must register your elements in a Namescope by using the RegisterName-Method. After that they are accessible via FindName. If you define your elements in XAML, this is automatically done for you.

    Code Snippet

     

    FlowDocument doc = new FlowDocument();

    NameScope.SetNameScope(doc, new NameScope());

    Paragraph par = new Paragraph();

    Span span;

    for (int i = 1; i <= 10; i++)

    {

    span = new Span(new Run(i.ToString()));

    span.Name = "x" + i.ToString(CultureInfo.InvariantCulture);

    par.Inlines.Add(span);

    doc.RegisterName(span.Name, span);

    }

    doc.Blocks.Add(par);

    span = doc.FindName("x3") as Span; // rets No more NULL!!

     

     

     

    An alternative is to use the FindLocigalNode-Method of LogicalTreeHelper-class. Internally this method searches from the specified element done the logical tree. the first element with the specified name is returned.

     

    Code Snippet

    FlowDocument doc = new FlowDocument();

    Paragraph par = new Paragraph();

    Span span;

    for (int i = 1; i <= 10; i++)

    {

    span = new Span(new Run(i.ToString()));

    span.Name = "x" + i.ToString(CultureInfo.InvariantCulture);

    par.Inlines.Add(span);

    }

    doc.Blocks.Add(par);

    span = LogicalTreeHelper.FindLogicalNode(doc,"x3") as Span;

     

     

    Saturday, March 29, 2008 2:35 PM
  • Thanks Thomas! These newsgroups are a blessing when learning new techs...
    Saturday, March 29, 2008 3:16 PM