我创建了一个带有设计时支持的简单自定义控件,如描述的这里。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.ComponentModel;
namespace CustomControlLibrary
{
public class ButtonWithDesignTime : Button
{
public ButtonWithDesignTime()
{
// The GetIsInDesignMode check and the following design-time
// code are optional and shown only for demonstration.
if (DesignerProperties.GetIsInDesignMode(this))
{
Content = "Design mode active";
}
}
}
}现在我需要知道在设计时设置的缩放因子是什么,但我不知道如何设置。

有什么我可以听的事件来发现这个吗?或者有什么财产可以揭示这一点?
发布于 2022-06-04 01:20:14
我不确定这是否是最优雅的方法,但这是我想出来的。这个问题已经困扰了我很久了!
private Point GetScaleFactor()
{
var rootVisual = PresentationSource.FromVisual(this)?.RootVisual;
if (rootVisual != null)
{
var transformToRoot = TransformToAncestor(rootVisual);
if (transformToRoot is Transform t)
{
return new Point(t.Value.M11, t.Value.M22);
}
}
return new Point(1, 1);
}https://stackoverflow.com/questions/70836721
复制相似问题