canvas内的中心图像

我在Canvas有一个Image

    

我希望在我的Canvas中心始终显示Image (即使在可能的大小调整之后),但是这样,图像就会在左上角绘制。 我能怎么做?

如果你改变图像和canvas的大小,那么这种方法最酷的东西就会起作用。 转换器代码:

 internal sealed class CenterConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { double canvasWidth = System.Convert.ToDouble(values[0]); double canvasHeight = System.Convert.ToDouble(values[1]); double controlWidth = System.Convert.ToDouble(values[2]); double controlHeight = System.Convert.ToDouble(values[3]); switch ((string)parameter) { case "top": return (canvasHeight - controlHeight) / 2; case "bottom": return (canvasHeight + controlHeight) / 2; case "left": return (canvasWidth - controlWidth) / 2; case "right": return (canvasWidth + controlWidth) / 2; default: return 0; } } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

XAML,资源中的某个地方:

   

XAML:

                     

Canvas旨在使用绝对坐标。 您可以使用dwrd提供的解决方案,或者将Canvas和Image都放在Grid中,然后将Image放在此Grid中。