如何使子元素填充其父元素

时间:2015-03-17 17:56:29

标签: c# wpf xaml

以下是我的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 &#x0a; fsm &#x0a; jklgf &#x0a;dmj &#x0a;dfm &#x0a;ljdgms &#x0a; kjlk &#x0a; jfdlkjsg"></TextBlock>
  </Grid>

感谢您的帮助

2 个答案:

答案 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 &#x0a; fsm &#x0a; jklgf &#x0a;dmj &#x0a;dfm &#x0a;ljdgms &#x0a; kjlk &#x0a; jfdlkjsg&#x0a; 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 &#x0a; fsm &#x0a; jklgf &#x0a;dmj &#x0a;dfm &#x0a;ljdgms&#x0a; jklgf &#x0a;dmj &#x0a;dfm &#x0a;ljdgms &#x0a; kjlk &#x0a; jfdlkjsg"></TextBlock>
        </Canvas>
    </Grid>        
</Grid>

希望有所帮助