There is actually a (subtle) difference between the two. Imagine you have the following code in File1.cs:
// File1.cs
using System;
namespace Outer.Inner
{
class Foo
{
static void Bar()
{
double d = Math.PI;
}
}
}
Now imagine that someone adds another file (File2.cs) to the project that looks like this:
// File2.cs
namespace Outer
{
class Math
{
}
}
The compiler searches Outer
before looking at those using
directives outside the namespace, so it finds Outer.Math
instead of System.Math
. Unfortunately (or perhaps fortunately?), Outer.Math
has no PI
member, so File1 is now broken.
This changes if you put the using
inside your namespace declaration, as follows:
// File1b.cs
namespace Outer.Inner
{
using System;
class Foo
{
static void Bar()
{
double d = Math.PI;
}
}
}
Now the compiler searches System
before searching Outer
, finds System.Math
, and all is well.
Some would argue that Math
might be a bad name for a user-defined class, since there's already one in System
; the point here is just that there is a difference, and it affects the maintainability of your code.
It's also interesting to note what happens if Foo
is in namespace Outer
, rather than Outer.Inner
. In that case, adding Outer.Math
in File2 breaks File1 regardless of where the using
goes. This implies that the compiler searches the innermost enclosing namespace before it looks at any using
directive.
If your InlineUIContainer is given a x:Name attribute, you can look for it specifically using this code:
if (rtf.Selection.Contains(myInlineUIContainer.ContentStart))
{...}
For more dynamic discovery you would need a loop something like this:
foreach (Block block in rtf.Document.Blocks)
{
Paragraph p = block as Paragraph;
if (p != null)
{
foreach (Inline inline in p.Inlines)
{
InlineUIContainer iuic = inline as InlineUIContainer;
if (iuic != null)
{
if (rtf.Selection.Contains(iuic.ContentStart))
{
Console.WriteLine("YES");
}
}
}
}
}
Best Answer
GetLineStartPosition
is able to return you the start of a line but not the end of a line. For that you will have to combine it withGetInsertionPosition
.Here's how
GetLineStartPosition
works:GetLineStartPosition(-1)
gets the start of the previous lineGetLineStartPosition(0)
gets the start of the current lineGetLineStartPosition(1)
gets the start of the next lineYou can also call it with larger integers to get lines further away.
To get the end of a line just get the start of the next line, then get the prior insertion position. Basically it is this:
However this does not work when you are on the last line of a document:
GetLineStartPosition
returns null.The easy way to fix it is to do this:
The reason
GetInsertionPosition
must be used rather than just moving one symbol over usingGetNextContextPosition
orGetPointerAtOffset
is that every element in theFlowDocument
element tree is a symbol. So for example if your current line is the last line in a table,GetLineStartPosition(1)
will return a pointer inside the first Run in the first Paragraph following the table, whereas the end of the current line is a the end of the last Run in the last Paragraph in the last TableCell, ... you get the idea.It is best to let WPF handle all the complexity of moving
TextPointers
around theFlowDocument
, which means usingGetInsertionPosition
to find the end of the same line the originalTextPointer
points to.