我已经在画布上了
<Canvas>
<Line X1="0" X2="{Binding ActualWidth, ElementName=uc}"
Y1="{Binding FirstPoint}" Y2="{Binding FirstPoint}"
Stroke="Red" StrokeThickness="1"/>
<TextBlock Text="{Binding FirstPoint}" />
<Line X1="0" X2="{Binding ActualWidth, ElementName=uc}"
Y1="{Binding SecondPoint}" Y2="{Binding SecondPoint}"
Stroke="Blue" StrokeThickness="1"/>
<TextBlock Text="{Binding SecondPoint}"/>
</Canvas>
我希望第一个TextBlock
出现在第一个Line
的右边,而第二个出现在第二个Line
的右边。
编辑
Canvas.Bottom="{Binding FirstPoint/SecondPoint}"
和Canvas.Right="0"
将它们放在我希望它们居中的行上方。
答案 0 :(得分:1)
设置Canvas.Top
并以垂直负Margin
调整最终位置:
<TextBlock Canvas.Top="{Binding FirstPoint}" Margin="0,-9"
Text="{Binding FirstPoint}" />
为了使其与特定的确切垂直边距无关,请使用具有足够大的边距且VerticalContentAlignment
设置为Center
的Label:
<Label Canvas.Top="{Binding FirstPoint}" Margin="0,-20"
Content="{Binding FirstPoint}"
VerticalContentAlignment="Center"/>
答案 1 :(得分:1)
好吧,因为WPF绑定不允许在绑定中进行数学运算。因此,您可以定义一个值转换器并将其用于居中放置文本框。
public class HalfValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var actual = (double) value;
return actual / 2;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
在您的XAML中:
<Canvas>
<Canvas.Resources>
<local:HalfValueConverter x:Key="HalfValueConverter"></local:HalfValueConverter>
</Canvas.Resources>
<Line X1="0" X2="{Binding ActualWidth, ElementName=uc}"
Y1="{Binding FirstPoint}" Y2="{Binding FirstPoint}" Name="Line1"
Stroke="Red" StrokeThickness="1"/>
<TextBlock Text="{Binding FirstPoint}" Canvas.Top="{Binding FirstPoint}" Margin="0,-20"
Canvas.Left="{Binding ActualWidth,ElementName=Line1,Converter={StaticResource HalfValueConverter}}"/>
<Line X1="0" X2="{Binding ActualWidth, ElementName=uc}"
Y1="{Binding SecondPoint}" Y2="{Binding SecondPoint}" Name="Line2"
Stroke="Blue" StrokeThickness="1"/>
<TextBlock Text="{Binding SecondPoint}" Canvas.Top="{Binding SecondPoint}" Margin="0,-20"
Canvas.Left="{Binding ActualWidth,ElementName=Line2,Converter={StaticResource HalfValueConverter}}"/>
</Canvas>