以下是我的xaml视图的摘录。 textBlock文本可能很长。但是我想让第0列(LineGeometry)中的行与此TextBlock的高度相同。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"></ColumnDefinition>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Path Grid.Column="0" Stroke="Black" StrokeThickness="3" Fill="Black">
<Path.Data>
<GeometryGroup>
<LineGeometry StartPoint="7,0" EndPoint="7,70"/>
<EllipseGeometry Center="7,20" RadiusX="5" RadiusY="5" />
</GeometryGroup>
</Path.Data>
</Path>
<TextBlock Grid.Column="1" Text="ldkfjlgdlgjljd 
 fsm 
 jklgf 
dmj 
dfm 
ljdgms 
 kjlk 
 jfdlkjsg"></TextBlock>
</Grid>
感谢您的帮助
答案 0 :(得分:2)
这符合您的要求
<Grid>
<Canvas>
<Border BorderBrush="#FFFF00" BorderThickness="2">
<StackPanel Orientation="Horizontal">
<Grid Margin="5,0">
<Border Width="2" Background="Black" />
<Ellipse Fill="Black" Width="12" Height="12" />
</Grid>
<TextBlock Text="ldkfjlgdlgjljd 
 fsm 
 jklgf 
dmj 
dfm 
ljdgms 
 kjlk 
 jfdlkjsg
 jfdlkjsg"></TextBlock>
</StackPanel>
</Border>
</Canvas>
</Grid>
答案 1 :(得分:1)
这是一个粗略的工作例子。我建议添加null检查等
首先,让我们创建一个返回Point
public class LineLengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var textBox = value as TextBlock;
double textHeight = textBox.ActualHeight;
var endpoint = new Point(7.0, textHeight);
return endpoint;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
现在是Xaml(我添加了所有的xaml,以便您可以将转换器的引用视为资源。
注意TextBlock
位于Canvas
小组中
和LineGeometry.EndPoint
的绑定
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="clr-namespace:WpfApplication9">
<Window.Resources>
<local:LineLengthConverter x:Key="LineLengthConverter" />
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"></ColumnDefinition>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid>
<Border BorderBrush="#FFFF00" BorderThickness="2">
<Path Stroke="Black" StrokeThickness="3" Fill="Black">
<Path.Data>
<GeometryGroup>
<LineGeometry StartPoint="7,0" EndPoint="{Binding ElementName=someText, Converter={StaticResource LineLengthConverter}}" />
<EllipseGeometry Center="7,20" RadiusX="5" RadiusY="5" />
</GeometryGroup>
</Path.Data>
</Path>
</Border>
<Canvas>
<TextBlock x:Name="someText" Text="ldkfjlgdlgjljd 
 fsm 
 jklgf 
dmj 
dfm 
ljdgms
 jklgf 
dmj 
dfm 
ljdgms 
 kjlk 
 jfdlkjsg"></TextBlock>
</Canvas>
</Grid>
</Grid>
希望有所帮助