why would GetPropertyValue return null when I just ran ApplyPropertyValue
-
Friday, August 17, 2012 11:35 PM
I use ApplyPropertyValue to apply a property to a TextRange. Then the next statement is GetPropertyValue of that TextRange. Why would I get back null for that property value? The property value is being successfully applied.
public List<TextRange> Lines { get; set; } public void HighlightLines() { WasBackProp = new object[this.Lines.Count]; int ix = 0; foreach (var tr in this.Lines) { var wasProp = tr.GetPropertyValue(TextElement.BackgroundProperty); WasBackProp[ix] = wasProp; tr.ApplyPropertyValue(TextElement.BackgroundProperty, "Yellow"); // threw this in the code. xx is coming back as null???? var xx = tr.GetPropertyValue(TextElement.BackgroundProperty); } }
All Replies
-
Saturday, August 18, 2012 4:40 AMIf you are reading data from stream then call TextRange.Save to make it persistent checkout MSDN documentation
-
Saturday, August 18, 2012 12:26 PM
If you are reading data from stream then call TextRange.Save to make it persistent checkout MSDN documentation
not a stream. The TextRanges apply to the FlowDocument of a RichTextBox.
-
Saturday, August 18, 2012 1:53 PM
The property you are attempting to set is a Brush not a string.
http://msdn.microsoft.com/en-us/library/system.windows.documents.textelement.background.aspx
Surprised that you don't get an error when attempting to set it, but as you can see it doesn't set the property to a type that is not.
Hope this helps
Lloyd Sheen
-
Saturday, August 18, 2012 3:00 PM
what i see there is you are making that code complex, use the following code instead:
tr.Background = new SolidBrush("color to be set");
This one line of code will do instead of that lengthy code
___________________________________________________________
Do vote the answer and propose the answer
- Proposed As Answer by kishhr Saturday, August 18, 2012 3:01 PM
-
Saturday, August 18, 2012 5:06 PM
Hi kishhr
We Appreciate your effort and contribution but we should not propose our own reply as proposed answer.
Thanks
Ashwini
-
Saturday, August 18, 2012 5:52 PM
what i see there is you are making that code complex, use the following code instead:
tr.Background = new SolidBrush("color to be set");
This one line of code will do instead of that lengthy code
but TextRange does not have a Background property.
-
Saturday, August 18, 2012 6:02 PM
The property you are attempting to set is a Brush not a string.
Hi Lloyd,
Thanks for the correction. But I changed the ApplyPropertyValue statement and GetPropertyValue still returns null.
publicList<TextRange> Lines
{
get;
set;
}
public void HighlightLines() { ResetBackProp(); WasBackProp = new object[this.Lines.Count]; int ix = 0; foreach (var tr in this.Lines) { // get and save the background property before the range is highlighted. var wasProp = tr.GetPropertyValue(TextElement.BackgroundProperty); WasBackProp[ix] = wasProp; tr.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.DarkRed); var xx = tr.GetPropertyValue(TextElement.BackgroundProperty); } }
- Marked As Answer by Steve Richter Saturday, August 18, 2012 6:02 PM
-
Saturday, August 18, 2012 7:10 PM
I created simple RichTextBox and used the following code (sorry VB) to test.
Private Sub Button_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Dim tr As TextRange = New TextRange(theRTB.Document.ContentStart, theRTB.Document.ContentEnd) tr.ApplyPropertyValue(TextBlock.BackgroundProperty, Brushes.Red) Dim t = tr.GetPropertyValue(TextBlock.BackgroundProperty) End SubWhat I found is that if there is text in the RTB then I get a value from the GetPropertyValue, but if there is no text I still get a TextRange but the GetPropertyValue now returns NULL/Nothing.
LS
Lloyd Sheen
- Marked As Answer by Steve Richter Monday, August 20, 2012 7:14 PM
-
Monday, August 20, 2012 7:17 PM
I created simple RichTextBox and used the following code (sorry VB) to test.
Private Sub Button_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Dim tr As TextRange = New TextRange(theRTB.Document.ContentStart, theRTB.Document.ContentEnd) tr.ApplyPropertyValue(TextBlock.BackgroundProperty, Brushes.Red) Dim t = tr.GetPropertyValue(TextBlock.BackgroundProperty) End SubWhat I found is that if there is text in the RTB then I get a value from the GetPropertyValue, but if there is no text I still get a TextRange but the GetPropertyValue now returns NULL/Nothing.
LS
Lloyd Sheen
I would add that null is a valid value when ApplyPropertyValue. Setting to null effectively clears the property value back to its default setting.

