使用IMultiValueConverter。
<Canvas Name="parent">
<Button Name="first" Width="10" Height="10"
Canvas.Top="30"/>
<Button Width="10" Height="10">
<Canvas.Top>
<MultiBinding Converter="{StaticResource CanvasConverter}" ConverterParameter="top">
<Binding ElementName="parent"></Binding>
<Binding ElementName="first"></Binding>
<Binding RelativeSource="{RelativeSource Mode=Self}"></Binding>
</MultiBinding>
</Canvas.Top>
</Button>
<Button Width="10" Height="10">
<Canvas.Top>
<MultiBinding Converter="{StaticResource CanvasConverter}" ConverterParameter="top">
<Binding ElementName="parent"></Binding>
<Binding ElementName="first"></Binding>
<Binding RelativeSource="{RelativeSource Mode=Self}"></Binding>
</MultiBinding>
</Canvas.Top>
</Button>
</Canvas>
有:
public class CanvasConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
Canvas can = values[0] as Canvas;
Button btn = values[1] as Button;
Button btn0 = values[2] as Button;
if (parameter.Equals("top"))
return Canvas.GetTop(btn) + can.Children.IndexOf(btn0) * 25;
else if (parameter.Equals("left"))
return Canvas.GetLeft(btn) + 0;
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
不要忘了資源的一部分:
<local:CanvasConverter x:Key="CanvasConverter" />
來源
2017-02-06 09:39:36
Ron
即使在綁定的情況下,您是爲每個按鈕tho手動設置邊距?我試圖讓coords/position動態 – DeveloperDoge
你可以使用IValueConverter嗎? – Ron
是的,如果這就是你要求的框架版本不是問題。 – DeveloperDoge