admin管理员组文章数量:1410682
I can set Paragraph's background property when creating a paragraph instance and add it to richTextBox.Document.Blocks
var p = new Paragraph
{
LineHeight = 1.8,
Background = _bgHighlight ;
}
var m = new Run("test string");
p.Inlines.Add(m);
richTextBox.Document.Blocks.Add(p);
After inserted a paragraph into Blocks, the background will not be able to be changed dynamically.
internal static readonly SolidColorBrush _bgHighlight = new SolidColorBrush(Colors.DodgerBlue) { Opacity = .4 };
private static readonly LinearGradientBrush _bgNormalManual = new LinearGradientBrush(Colors.Wheat, Colors.Coral, new Point(0, 0), new Point(0, 1)) { Opacity = .5 };
richTextBox.PreviewMouseLeftButtonDown += (s, e) => {
Paragraph p = null;
if (e.OriginalSource is Paragraph x)
{
p = x;
}
else if (e.OriginalSource is Run y && y.Parent is Paragraph z)
{
p = z;
}
if (p != null)
{
p.Background = _bgNormalManual;
}
};
I have searched around, just fond only TextRange's Background can be changed.
var textRange = new TextRange(p.ContentStart, p.ContentEnd);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, _bgNormal);
but, it only can change the text words's background, not paragraph's background.
I can set Paragraph's background property when creating a paragraph instance and add it to richTextBox.Document.Blocks
var p = new Paragraph
{
LineHeight = 1.8,
Background = _bgHighlight ;
}
var m = new Run("test string");
p.Inlines.Add(m);
richTextBox.Document.Blocks.Add(p);
After inserted a paragraph into Blocks, the background will not be able to be changed dynamically.
internal static readonly SolidColorBrush _bgHighlight = new SolidColorBrush(Colors.DodgerBlue) { Opacity = .4 };
private static readonly LinearGradientBrush _bgNormalManual = new LinearGradientBrush(Colors.Wheat, Colors.Coral, new Point(0, 0), new Point(0, 1)) { Opacity = .5 };
richTextBox.PreviewMouseLeftButtonDown += (s, e) => {
Paragraph p = null;
if (e.OriginalSource is Paragraph x)
{
p = x;
}
else if (e.OriginalSource is Run y && y.Parent is Paragraph z)
{
p = z;
}
if (p != null)
{
p.Background = _bgNormalManual;
}
};
I have searched around, just fond only TextRange's Background can be changed.
var textRange = new TextRange(p.ContentStart, p.ContentEnd);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, _bgNormal);
but, it only can change the text words's background, not paragraph's background.
Share Improve this question asked 2 days ago longlong 661 silver badge9 bronze badges1 Answer
Reset to default 0I have found a solution, using DrawingBrush. for example
<DrawingBrush x:Key="paragraphSelectedBg">
<DrawingBrush.Drawing>
<GeometryDrawing Brush="LightBlue">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
internal static readonly DrawingBrush _paragraphSelectedBg= Application.Current.FindResource("paragraphSelectedBg") as DrawingBrush;
p.Background = _paragraphUnselectedBg;
本文标签: WPFhow to dynamic change Paragraph39s backgroundStack Overflow
版权声明:本文标题:wpf , how to dynamic change Paragraph's background - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736652412a1946172.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论