以下代码改编自发布了 WPF 4.5 的 Adam Nathan 第 502 页上的代码示例。
按照那里的建议,我创建了一个VisualHostClass
“托管”绘图视觉列表的类,从而使它们可显示。到目前为止,这很有效。
现在我想通过更改显示的视觉效果的颜色来对鼠标左键单击做出反应。代码在方法中HitTestCallback
。
使用包含的 Trace.WriteLine 语句,我可以看到相关行
drw.Brush = Brushes.Red;
达到,然后属性 Brush 具有新值。但是:显示没有改变,点击后的DrawingVisual仍然是黄色(这是创建时使用的画笔颜色)。
我可以/应该在这里做什么?(我是 WPF 的绝对初学者)。
提前感谢所有答案!
public partial class VisualHostClass : FrameworkElement
{
public VisualHostClass()
{
}
public List<DrawingVisual> myVisuals = new List<DrawingVisual>();
public void AddVisual(DrawingVisual dvs)
{
myVisuals.Add(dvs);
AddVisualChild(dvs);
AddLogicalChild(dvs);
}
protected override int VisualChildrenCount
{
get { return myVisuals.Count; }
}
protected override Visual GetVisualChild(int index)
{
if (index < 0 || index >= myVisuals.Count)
{
throw new ArgumentOutOfRangeException("index");
}
return myVisuals[index];
}
public void removeAllChilds()
{
foreach (DrawingVisual dvs in myVisuals)
{
RemoveVisualChild(dvs);
RemoveLogicalChild(dvs);
}
myVisuals.Clear();
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
Point location = e.GetPosition(this);
VisualTreeHelper.HitTest(this, null,
new HitTestResultCallback(HitTestCallback),
new PointHitTestParameters(location));
}
public HitTestResultBehavior HitTestCallback(HitTestResult result)
{
if (result.VisualHit.GetType() == typeof(DrawingVisual))
{
DrawingVisual dv = result.VisualHit as DrawingVisual;
Trace.WriteLine("HitTestCallback: dv = " + dv.ToString());
foreach(GeometryDrawing drw in dv.Drawing.Children)
{
Trace.WriteLine("HitTestCallback: drw = " + drw.ToString());
Trace.WriteLine("HitTestCallback: drw.Geometry = " + drw.Geometry.ToString());
Trace.WriteLine("HitTestCallback: drw.Brush = " + drw.Brush.ToString());
drw.Brush = Brushes.Red;
Trace.WriteLine("HitTestCallback: drw.Brush = " + drw.Brush.ToString());
Trace.WriteLine("HitTestCallback: drw.IsFrozen = " + drw.IsFrozen.ToString());
}
}
return HitTestResultBehavior.Continue;
}
}
}
编辑:经过一些试验后,以下工作(但这对我来说并不令人满意,因为我只能改变Color
a SolidColorBrush
,但不能改变Brush
它本身):
public void ChangeBrush(DrawingVisual dv)
{
Trace.WriteLine("HitTestCallback: dv = " + dv.ToString());
foreach (Drawing drw1 in dv.Drawing.Children)
{
if (drw1 is GeometryDrawing)
{
GeometryDrawing drw = drw1 as GeometryDrawing;
Trace.WriteLine("HitTestCallback: drw = " + drw.ToString());
Trace.WriteLine("HitTestCallback: drw.Geometry = " + drw.Geometry.ToString());
Trace.WriteLine("HitTestCallback: drw.Brush = " + drw.Brush.ToString());
Trace.WriteLine("HitTestCallback: drw.IsFrozen = " + drw.IsFrozen.ToString());
Trace.WriteLine("HitTestCallback: drw.Brush.IsFrozen = " + drw.Brush.IsFrozen.ToString());
// does not work
//SolidColorBrush newBrush = new SolidColorBrush(Colors.Red);
//newBrush.Freeze();
//drw.Brush = newBrush;
// works with drw.Brush already containing a SolidColorBrush
(drw.Brush as SolidColorBrush).Color =
(drw.Brush as SolidColorBrush).Color == Colors.LightGoldenrodYellow ?
Colors.Red : Colors.LightGoldenrodYellow;
Trace.WriteLine("HitTestCallback: drw.Brush = " + drw.Brush.ToString());
Trace.WriteLine("HitTestCallback: drw.IsFrozen = " + drw.IsFrozen.ToString());
Trace.WriteLine("HitTestCallback: drw.Brush.IsFrozen = " + drw.Brush.IsFrozen.ToString());
}
}
}
只是为了记录,我引用了DrawingVisual
构造和放入的代码VisualHostClass
PathGeometry pgnGeom = RenderPolygonWithHoles2Geometry(pgn);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
SolidColorBrush brush1 = new SolidColorBrush(Colors.LightGoldenrodYellow);
dc.DrawGeometry(brush1, null, pgnGeom);
}
myVisualHost.AddVisual(dv);