Самая крутая вещь этого aproach taht это сработает, если вы измените размеры изображения и холста. конвертер код:
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, где-то в ресурсах:
<local:CenterConverter x:Key="CenterConverter" />
XAML:
<Canvas x:Name="superCoolCanvas">
<Image x:Name="superCoolImage" >
<Canvas.Top>
<MultiBinding Converter="{StaticResource CenterConverter}" ConverterParameter="top">
<Binding ElementName="superCoolCanvas" Path="ActualWidth" />
<Binding ElementName="superCoolCanvas" Path="ActualHeight" />
<Binding ElementName="superCoolImage" Path="ActualWidth" />
<Binding ElementName="superCoolImage" Path="ActualHeight" />
</MultiBinding>
</Canvas.Top>
<Canvas.Left>
<MultiBinding Converter="{StaticResource CenterConverter}" ConverterParameter="left">
<Binding ElementName="superCoolCanvas" Path="ActualWidth" />
<Binding ElementName="superCoolCanvas" Path="ActualHeight" />
<Binding ElementName="superCoolImage" Path="ActualWidth" />
<Binding ElementName="superCoolImage" Path="ActualHeight" />
</MultiBinding>
</Canvas.Left>
</Image>
</Canvas>
Я думаю, что это может быть решен с помощью MultiBinding с реагированием конвертора – dvvrd
возможного дубликата http://stackoverflow.com/questions/2208992/how-to-center-an-element-in-wpf-canvas –
@dvvrd Покажите мне, как – Nick