wpf – 如何在DrawingGroup.Children标记中更改笔的笔刷颜色

我正在尝试从另一个控件的下一段代码中更改属性Brush,但不知道如何到达此属性.

此绘图笔刷定义为Application.Resource和App.XAML

<DrawingBrush x:Key="Disp_Origin" Stretch="Uniform">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <DrawingGroup.Children>
                    <GeometryDrawing Geometry="F1 M 720.099,497.862C 778.822,493.976 837.662,492.02 896.514,492.02">
                        <GeometryDrawing.Pen>
                            <Pen Thickness="1.33333" MiterLimit="2.75" Brush="#FFA5AEB7"/>
                        </GeometryDrawing.Pen>
                    </GeometryDrawing>...

当另一个控件聚焦或悬停时,我需要达到笔刷属性.

我不知道这是否可行

最佳答案 其实我已经找到了如何做到这一点.但它需要背后的代码.最重要的是它允许更改控件资源的Clone()方法,你应该从后面的代码设置这个画笔资源.不幸的是,代码的可读性不高.

// Initializing code
// And as mentioned in comments code for restore color
DrawingBrush myBrush = ( Application.Current.Resources["Disp_Origin"] as DrawingBrush ).Clone();
testButton.Background = myBrush;

// On hover or on focus code
DrawingBrush settedBrush = testButton.Background as DrawingBrush;
( ( System.Windows.Media.GeometryDrawing )( ( ( System.Windows.Media.DrawingGroup )
   ( settedBrush.Drawing ) ).Children[0] ) ).Pen.Brush = new SolidColorBrush( Colors.Red );

更新

正如评论中所提到的,当控件未被聚焦或覆盖时,它需要恢复画笔.

点赞