本文整理汇总了C#中System.Windows.Documents.TextPointer.GetAdjacentElement方法的典型用法代码示例。如果您正苦于以下问题:C# TextPointer.GetAdjacentElement方法的具体用法?C# TextPointer.GetAdjacentElement怎么用?C# TextPointer.GetAdjacentElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Documents.TextPointer
的用法示例。
在下文中一共展示了TextPointer.GetAdjacentElement方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindElementPosition
// ------------------------------------------------------------------
// IContentHost Helpers
// ------------------------------------------------------------------
/// <summary>
/// Searches for an element in the _complexContent.TextContainer. If the element is found, returns the
/// position at which it is found. Otherwise returns null.
/// </summary>
/// <param name="e">
/// Element to be found.
/// </param>
/// <remarks>
/// We assume that this function is called from within text if the caller knows that _complexContent exists
/// and contains a TextContainer. Hence we assert for this condition within the function
/// </remarks>
private TextPointer FindElementPosition(IInputElement e)
{
// Parameter validation
Debug.Assert(e != null);
// Validate that this function is only called when a TextContainer exists as complex content
Debug.Assert(_complexContent.TextContainer is TextContainer);
TextPointer position;
// If e is a TextElement we can optimize by checking its TextContainer
if (e is TextElement)
{
if ((e as TextElement).TextContainer == _complexContent.TextContainer)
{
// Element found
position = new TextPointer((e as TextElement).ElementStart);
return position;
}
}
// Else: search for e in the complex content
position = new TextPointer((TextPointer)_complexContent.TextContainer.Start);
while (position.CompareTo((TextPointer)_complexContent.TextContainer.End) < 0)
{
// Search each position in _complexContent.TextContainer for the element
switch (position.GetPointerContext(LogicalDirection.Forward))
{
case TextPointerContext.EmbeddedElement:
DependencyObject embeddedObject = position.GetAdjacentElement(LogicalDirection.Forward);
if (embeddedObject is ContentElement || embeddedObject is UIElement)
{
if (embeddedObject == e as ContentElement || embeddedObject == e as UIElement)
{
return position;
}
}
break;
default:
break;
}
position.MoveByOffset(+1);
}
// Reached end of complex content without finding the element
return null;
}
示例2: ValidateMergingPositions
// Validates that the sibling element at this position belong to expected itemType (Inline, Block, ListItem)
private static void ValidateMergingPositions(Type itemType, TextPointer start, TextPointer end)
{
if (start.CompareTo(end) < 0)
{
// Verify inner part
TextPointerContext forwardFromStart = start.GetPointerContext(LogicalDirection.Forward);
TextPointerContext backwardFromEnd = end.GetPointerContext(LogicalDirection.Backward);
Invariant.Assert(forwardFromStart == TextPointerContext.ElementStart, "Expecting first opening tag of pasted fragment");
Invariant.Assert(backwardFromEnd == TextPointerContext.ElementEnd, "Expecting last closing tag of pasted fragment");
Invariant.Assert(itemType.IsAssignableFrom(start.GetAdjacentElement(LogicalDirection.Forward).GetType()), "The first pasted fragment item is expected to be a " + itemType.Name);
Invariant.Assert(itemType.IsAssignableFrom(end.GetAdjacentElement(LogicalDirection.Backward).GetType()), "The last pasted fragment item is expected to be a " + itemType.Name);
// Veryfy outer part
TextPointerContext backwardFromStart = start.GetPointerContext(LogicalDirection.Backward);
TextPointerContext forwardFromEnd = end.GetPointerContext(LogicalDirection.Forward);
Invariant.Assert(backwardFromStart == TextPointerContext.ElementStart || backwardFromStart == TextPointerContext.ElementEnd || backwardFromStart == TextPointerContext.None, "Bad context preceding a pasted fragment");
Invariant.Assert(!(backwardFromStart == TextPointerContext.ElementEnd) || itemType.IsAssignableFrom(start.GetAdjacentElement(LogicalDirection.Backward).GetType()), "An element preceding a pasted fragment is expected to be a " + itemType.Name);
Invariant.Assert(forwardFromEnd == TextPointerContext.ElementStart || forwardFromEnd == TextPointerContext.ElementEnd || forwardFromEnd == TextPointerContext.None, "Bad context following a pasted fragment");
Invariant.Assert(!(forwardFromEnd == TextPointerContext.ElementStart) || itemType.IsAssignableFrom(end.GetAdjacentElement(LogicalDirection.Forward).GetType()), "An element following a pasted fragment is expected to be a " + itemType.Name);
}
}
示例3: SetCell
public void SetCell(BufferCell cell, TextPointer position, LogicalDirection direction)
{
TextRange range = null;
TextPointer positionPlus = position.GetNextInsertionPosition(LogicalDirection.Forward);
if (positionPlus != null)
{
range = new TextRange(position, positionPlus);
}
if (null == range || range.IsEmpty)
{
position = position.GetInsertionPosition(LogicalDirection.Forward);
if (position != null)
{
Run r = position.GetAdjacentElement(LogicalDirection.Forward) as Run;
if (null != r)
{
if (r.Text.Length > 0)
{
char[] chr = r.Text.ToCharArray();
chr[0] = cell.Character;
r.Text = chr.ToString();
}
else
{
r.Text = cell.Character.ToString();
}
}
else
{
r = position.GetAdjacentElement(LogicalDirection.Backward) as Run;
if (null != r
&& r.Background == BrushFromConsoleColor(cell.BackgroundColor)
&& r.Foreground == BrushFromConsoleColor(cell.ForegroundColor)
)
{
if (r.Text.Length > 0)
{
r.Text = r.Text + cell.Character;
}
else
{
r.Text = cell.Character.ToString();
}
}
else
{
r = new Run(cell.Character.ToString(), position);
}
}
r.Background = BrushFromConsoleColor(cell.BackgroundColor);
r.Foreground = BrushFromConsoleColor(cell.ForegroundColor);
//position = r.ElementStart;
}
}
else
{
range.Text = cell.Character.ToString();
range.ApplyPropertyValue(TextElement.BackgroundProperty, BrushFromConsoleColor(cell.BackgroundColor));
range.ApplyPropertyValue(TextElement.ForegroundProperty, BrushFromConsoleColor(cell.ForegroundColor));
}
}
示例4: FindElementPosition
//-------------------------------------------------------------------
// IContentHost Helpers
//-------------------------------------------------------------------
/// <summary>
/// Searches for an element in the _structuralCache.TextContainer. If the element is found, returns the
/// position at which it is found. Otherwise returns null.
/// </summary>
/// <param name="e">
/// Element to be found.
/// </param>
/// <param name="isLimitedToTextView">
/// bool value indicating whether the search should only be limited to the text view of the page,
/// in which case we search only text segments in the text view
/// </param>
private TextPointer FindElementPosition(IInputElement e, bool isLimitedToTextView)
{
// Parameter validation
Debug.Assert(e != null);
// Validate that this function is only called when a TextContainer exists as complex content
Debug.Assert(_structuralCache.TextContainer is TextContainer);
TextPointer elementPosition = null;
// If e is a TextElement we can optimize by checking its TextContainer
if (e is TextElement)
{
if ((e as TextElement).TextContainer == _structuralCache.TextContainer)
{
// Element found
elementPosition = new TextPointer((e as TextElement).ElementStart);
}
// else: elementPosition stays null
}
else
{
// Else: search for e in the complex content
if (!(_structuralCache.TextContainer.Start is TextPointer) ||
!(_structuralCache.TextContainer.End is TextPointer))
{
// Invalid TextContainer, don't search
return null;
}
TextPointer searchPosition = new TextPointer(_structuralCache.TextContainer.Start as TextPointer);
while (elementPosition == null && ((ITextPointer)searchPosition).CompareTo(_structuralCache.TextContainer.End) < 0)
{
// Search each position in _structuralCache.TextContainer for the element
switch (searchPosition.GetPointerContext(LogicalDirection.Forward))
{
case TextPointerContext.EmbeddedElement:
DependencyObject embeddedObject = searchPosition.GetAdjacentElement(LogicalDirection.Forward);
if (embeddedObject is ContentElement || embeddedObject is UIElement)
{
if (embeddedObject == e as ContentElement || embeddedObject == e as UIElement)
{
// Element found. Stop searching
elementPosition = new TextPointer(searchPosition);
break;
}
}
break;
default:
break;
}
searchPosition.MoveToNextContextPosition(LogicalDirection.Forward);
}
}
// If the element was found, check if we are limited to text view
if (elementPosition != null)
{
if (isLimitedToTextView)
{
// At this point, we should create TextView if it doesn't exist
_textView = GetTextView();
Invariant.Assert(_textView != null);
// Check all segements in text view for position
for (int segmentIndex = 0; segmentIndex < ((ITextView)_textView).TextSegments.Count; segmentIndex++)
{
if (((ITextPointer)elementPosition).CompareTo(((ITextView)_textView).TextSegments[segmentIndex].Start) >= 0 &&
((ITextPointer)elementPosition).CompareTo(((ITextView)_textView).TextSegments[segmentIndex].End) < 0)
{
// Element lies within a segment. Return position
return elementPosition;
}
}
// Element not found in all segments of TextView. Set position to null
elementPosition = null;
}
}
return elementPosition;
}
示例5: MergeFormattingInlines
// Merges inline elements with equivalent formatting properties at a given position
// Returns true if some changes happened at this position, false otherwise
internal static bool MergeFormattingInlines(TextPointer position)
{
// Remove unnecessary Spans around this position
RemoveUnnecessarySpans(position);
// Delete empty formatting elements at this position (if any)
ExtractEmptyFormattingElements(position);
// Skip formatting tags towards potential merging position
while (position.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart &&
TextSchema.IsMergeableInline(position.Parent.GetType()))
{
position = ((Inline)position.Parent).ElementStart;
}
while (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd &&
TextSchema.IsMergeableInline(position.Parent.GetType()))
{
position = ((Inline)position.Parent).ElementEnd;
}
// Merge formatting Inlines at this position
Inline firstInline, secondInline;
bool merged = false;
while (
position.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementEnd &&
position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementStart &&
(firstInline = position.GetAdjacentElement(LogicalDirection.Backward) as Inline) != null &&
(secondInline = position.GetAdjacentElement(LogicalDirection.Forward) as Inline) != null)
{
if (TextSchema.IsFormattingType(firstInline.GetType()) && firstInline.TextRange.IsEmpty)
{
firstInline.RepositionWithContent(null);
merged = true;
}
else if (TextSchema.IsFormattingType(secondInline.GetType()) && secondInline.TextRange.IsEmpty)
{
secondInline.RepositionWithContent(null);
merged = true;
}
else if (TextSchema.IsKnownType(firstInline.GetType()) && TextSchema.IsKnownType(secondInline.GetType()) &&
(firstInline is Run && secondInline is Run || firstInline is Span && secondInline is Span) &&
TextSchema.IsMergeableInline(firstInline.GetType()) && TextSchema.IsMergeableInline(secondInline.GetType())
&& CharacterPropertiesAreEqual(firstInline, secondInline))
{
firstInline.Reposition(firstInline.ElementStart, secondInline.ElementEnd);
secondInline.Reposition(null, null);
merged = true;
}
else
{
break;
}
}
// Now that Inlines have been merged we can try to optimize tree structure
// by eliminating some unecessary wrapping Inlines
if (merged)
{
RemoveUnnecessarySpans(position);
}
return merged;
}
示例6: MergeLists
/// <summary>
/// Merges two naighboring lists ending and starting at position mergePosition
/// </summary>
/// <param name="mergePosition">
/// Position at with two List elements are expected to appear next to each other
/// </param>
/// <returns>
/// true if there were two mergeable List elements and merge happened.
/// false if there is no pair of List elements at the mergePosition.
/// </returns>
internal static bool MergeLists(TextPointer mergePosition)
{
if (mergePosition.GetPointerContext(LogicalDirection.Backward) != TextPointerContext.ElementEnd ||
mergePosition.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.ElementStart)
{
return false;
}
List precedingList = mergePosition.GetAdjacentElement(LogicalDirection.Backward) as List;
List followingList = mergePosition.GetAdjacentElement(LogicalDirection.Forward) as List;
if (precedingList == null || followingList == null)
{
return false;
}
precedingList.Reposition(precedingList.ContentStart, followingList.ElementEnd);
followingList.Reposition(null, null);
// We need to set appropriate FlowDirection property on the new List and its paragraph children.
// We take the FlowDirection value from the preceding list.
TextRangeEdit.SetParagraphProperty(precedingList.ElementStart, precedingList.ElementEnd,
Paragraph.FlowDirectionProperty, precedingList.GetValue(Paragraph.FlowDirectionProperty));
return true;
}
示例7: ApplyStructuralInlinePropertyAcrossRun
private static void ApplyStructuralInlinePropertyAcrossRun(TextPointer start, TextPointer end, Run run, DependencyProperty formattingProperty, object value)
{
if (start.CompareTo(end) == 0)
{
// When the range is empty we should ignore the command, except
// for the case of empty Run which can be encountered in empty paragraphs
if (run.IsEmpty)
{
run.SetValue(formattingProperty, value);
}
}
else
{
// Split elements at start and end boundaries.
start = SplitFormattingElements(start, /*keepEmptyFormatting:*/false, /*limitingAncestor*/run.Parent as TextElement);
end = SplitFormattingElements(end, /*keepEmptyFormatting:*/false, /*limitingAncestor*/run.Parent as TextElement);
run = (Run)start.GetAdjacentElement(LogicalDirection.Forward);
run.SetValue(formattingProperty, value);
}
// Clear property value from all ancestors of this Run.
FixupStructuralPropertyEnvironment(run, formattingProperty);
}
示例8: ApplyStructuralInlinePropertyAcrossInline
private static void ApplyStructuralInlinePropertyAcrossInline(TextPointer start, TextPointer end, TextElement commonAncestor, DependencyProperty formattingProperty, object value)
{
start = SplitFormattingElements(start, /*keepEmptyFormatting:*/false, commonAncestor);
end = SplitFormattingElements(end, /*keepEmptyFormatting:*/false, commonAncestor);
DependencyObject forwardElement = start.GetAdjacentElement(LogicalDirection.Forward);
DependencyObject backwardElement = end.GetAdjacentElement(LogicalDirection.Backward);
if (forwardElement == backwardElement &&
(forwardElement is Run || forwardElement is Span))
{
// After splitting we have exactly one Run or Span between start and end. Use it for setting the property.
Inline inline = (Inline)start.GetAdjacentElement(LogicalDirection.Forward);
// Set the property to existing element.
inline.SetValue(formattingProperty, value);
// Clear property value from all ancestors of this inline.
FixupStructuralPropertyEnvironment(inline, formattingProperty);
if (forwardElement is Span)
{
// Clear property value from all Span and Run children of this span.
ClearPropertyValueFromSpansAndRuns(inline.ContentStart, inline.ContentEnd, formattingProperty);
}
}
else
{
Span span;
if (commonAncestor is Span &&
start.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart &&
end.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd &&
start.GetAdjacentElement(LogicalDirection.Backward) == commonAncestor)
{
// Special case when start and end are at parent Span boundaries.
// Don't need to create a new Span in this case.
span = (Span)commonAncestor;
}
else
{
// Create a new span from start to end.
span = new Span();
span.Reposition(start, end);
}
// Set property on the span.
span.SetValue(formattingProperty, value);
// Clear property value from all ancestors of this span.
FixupStructuralPropertyEnvironment(span, formattingProperty);
// Clear property value from all Span and Run children of this span.
ClearPropertyValueFromSpansAndRuns(span.ContentStart, span.ContentEnd, formattingProperty);
}
}
示例9: GetNextRun
// Finds a Run element with ElementStart at or after the given pointer
// Creates Runs at potential run positions if encounters some.
private static Run GetNextRun(TextPointer pointer, TextPointer limit)
{
Run run = null;
while (pointer != null && pointer.CompareTo(limit) < 0)
{
if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementStart &&
(run = pointer.GetAdjacentElement(LogicalDirection.Forward) as Run) != null)
{
break;
}
if (TextPointerBase.IsAtPotentialRunPosition(pointer))
{
pointer = TextRangeEditTables.EnsureInsertionPosition(pointer);
Invariant.Assert(pointer.Parent is Run);
run = pointer.Parent as Run;
break;
}
// Advance the scanning pointer
pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
}
return run;
}
示例10: GetNextIMEVisibleNode
// Helper for DeleteContentInternal.
// If startPosition is placed at the very front of its parent's sibling list,
// returns the next sibling following endPositoin (the new head of the sibling
// list). The new head node is interesting because its IMELeftEdgeCharCount may
// change because of its new position.
private TextTreeTextElementNode GetNextIMEVisibleNode(TextPointer startPosition, TextPointer endPosition)
{
TextTreeTextElementNode nextIMEVisibleNode = null;
TextElement adjacentElement = startPosition.GetAdjacentElement(LogicalDirection.Forward) as TextElement;
if (adjacentElement != null && adjacentElement.IsFirstIMEVisibleSibling)
{
nextIMEVisibleNode = (TextTreeTextElementNode)endPosition.GetAdjacentSiblingNode(LogicalDirection.Forward);
}
return nextIMEVisibleNode;
}
示例11: searchHyperlink
/// <summary>
/// Test if the text pointer is in a hyperlink
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
private bool searchHyperlink(TextPointer pos)
{
if (null == pos)
return false;
TextPointerContext context = pos.GetPointerContext(LogicalDirection.Backward);
if (null == context)
return false;
if (TextPointerContext.ElementStart == context)
{
object elem = pos.GetAdjacentElement(LogicalDirection.Backward);
if (elem is Hyperlink)
{
return true;
}
}
else if (TextPointerContext.ElementEnd == context)
{
object elem = pos.GetAdjacentElement(LogicalDirection.Backward);
if (elem is Hyperlink)
{
return false;
}
}
// go backwards...
TextPointer back = pos.GetNextContextPosition(LogicalDirection.Backward);
return searchHyperlink(back);
}
示例12: WriteContainer
/// <summary>
/// Writes the container into the specified XmlWriter.
/// </summary>
private void WriteContainer(TextPointer start, TextPointer end, XmlWriter writer)
{
TextElement textElement;
System.Diagnostics.Debug.Assert(start != null);
System.Diagnostics.Debug.Assert(end != null);
System.Diagnostics.Debug.Assert(writer != null);
_writer = writer;
WriteWordXmlHead();
_cursor = start;
while (_cursor.CompareTo(end) < 0)
{
switch (_cursor.GetPointerContext(_dir))
{
case TextPointerContext.None:
System.Diagnostics.Debug.Assert(false,
"Next symbol should never be None if cursor < End.");
break;
case TextPointerContext.Text:
RequireOpenRange();
_writer.WriteStartElement(WordXmlSerializer.WordTextTag);
_writer.WriteString(_cursor.GetTextInRun(_dir));
_writer.WriteEndElement();
break;
case TextPointerContext.EmbeddedElement:
DependencyObject obj = _cursor.GetAdjacentElement(LogicalDirection.Forward);
if (obj is LineBreak)
{
RequireOpenRange();
_writer.WriteStartElement(WordXmlSerializer.WordBreakTag);
_writer.WriteEndElement();
}
// TODO: try to convert some known embedded objects.
break;
case TextPointerContext.ElementStart:
TextPointer position;
position = _cursor;
position = position.GetNextContextPosition(LogicalDirection.Forward);
textElement = position.Parent as TextElement;
if (textElement is Paragraph)
{
RequireClosedRange();
RequireOpenParagraph();
}
else if (textElement is Inline)
{
RequireClosedRange();
RequireOpenParagraph();
RequireOpenRange();
}
break;
case TextPointerContext.ElementEnd:
textElement = _cursor.Parent as TextElement;
if (textElement is Inline)
{
RequireClosedRange();
}
else if (textElement is Paragraph)
{
RequireClosedParagraph();
}
break;
}
_cursor = _cursor.GetNextContextPosition(_dir);
}
RequireClosedRange();
WriteWordXmlTail();
}
示例13: GetParagraphsBetweenPositions
private static IEnumerable<Paragraph> GetParagraphsBetweenPositions(TextPointer start, TextPointer end)
{
var changedParas = new HashSet<Paragraph>();
var para = start.Paragraph ?? start.GetAdjacentElement(LogicalDirection.Forward) as Paragraph;
while (para != null && para.ContentStart.CompareTo(end) < 0)
{
changedParas.Add(para);
para = para.NextBlock as Paragraph;
}
return changedParas;
}