1

我的应用程序中有拖放支持。当我开始拖动某个项目时,我会从代码中创建拖动视觉效果。下面的代码有效,但我想使用样式设置宽度和高度,而不是像下面这样硬编码。如何?

private void OnDragInitialize(object sender, DragInitializeEventArgs args)
{
    args.AllowedEffects = DragDropEffects.All;

    var shape = new MyShape();

    // TODO: How do I let the 'DragShapeStyle' style apply the dimensions?
    var shapeSize = new Size(180, 80);

    var dropInfo = new DiagramDropInfo(shapeSize, SerializeItems(
        new List<IDiagramItem> { shape }));

    args.Data = dropInfo;
    args.DragVisualOffset = new Point(
        args.RelativeStartPoint.X - (shapeSize.Width / 2),
        args.RelativeStartPoint.Y - (shapeSize.Height / 2));

    var dragShape = new DragShape()
    {
        DataContext = new ShapeData()
        {
            Data = new MyData()
        }
    };

    dragShape.SetResourceReference(DragShape.StyleProperty, "DragShapeStyle");

    args.DragVisual = new ContentControl() { Content = dragShape };
}

我的DragShapeStyle风格绘制了一个网格,它的大小使用属性MyData和其他一些逻辑来设置。我想将此逻辑应用于我拖动的 Visual。

4

0 回答 0