0
我創建一個自定義Shape
類:創建形狀類內部的鼠標事件
public class CustomBox : Shape
{
protected override Geometry DefiningGeometry
{
get
{
Stroke = Brushes.Red;
var pathGeometry = new PathGeometry();
pathGeometry.AddGeometry(new RectangleGeometry(new Rect(new Point(100, 200), new Point(400, 400)));
var formattedText = new FormattedText("CustomBox", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Times New-Roman"), 14, Brushes.Red);
var textGeometry = formattedText.BuildGeometry(new Point(100, 200));
pathGeometry.AddGeometry(textGeometry);
return pathGeometry;
}
}
}
我想補充/覆蓋/實施內部Mouse
事件如MouseDown
,MouseUp
,MouseMove
等將定義CustomBox
對象的行爲。
例如:
我想補充時,我將鼠標懸停在對象時發生的「行爲」 - 其結果將是,該Rectangle
將被突出顯示(通過增加StrokeThickness
例如)。
可以添加這樣的功能,而不必將對象註冊到Mouse
事件外部(創建CustomBox
並將其添加到Canvas
的類)?
看起來像XY問題。您的示例可以作爲樣式觸發器來實現。如果您想對其他鼠標事件作出反應,只需重寫適當的'OnPreviewMouse ***'/'OnMouse ***'方法,例如'OnMouseMove'。 – Dennis