GilShalit
GilShalit

Reputation: 6463

WPF: TextBlock with Wrapped text should inherit width

I have the following XAML:

            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Border Name="B1" Width="Auto">
                        <DataGrid />
                    </Border>
                    <Border Name="B2" Width="Auto">
                        <DataGrid />
                    </Border>
                </StackPanel>
                <Border Name="B3" Width="Auto">
                    <TextBlock TextWrapping="Wrap"
                               Text="{Binding}"/>
                </Border>
            </StackPanel>

Basically, borders B1 and B2 are side by side on top, and B3 is below them. B1 and B2 change their widths dynamically according to stuff going on in their contents. I want the width of B3 to be the sum of the widths of B1 and B2, with word wrapping of the potentially long text in the TextBlock.

The XAML above results in the text in the TextBlock not to wrap, which results in B3 being much wider then B1+B2.

I want B3.width to inherit from the main StackPanel's width, which will be set by B1.width+B2.width, and I would like to do it without code behind if possible.

Any ideas?

Upvotes: 0

Views: 2655

Answers (1)

Kishore Kumar
Kishore Kumar

Reputation: 21863

<StackPanel x:Name="stk">
    <StackPanel Orientation="Horizontal" x:Name="stk1" HorizontalAlignment="Left" >
        <Border Name="B1"
                Width="Auto">
            <ListBox Width="200" Height="200"></ListBox>
        </Border >
        <Border Name="B2"
                Width="Auto">
            <ListBox Width="200"></ListBox>
        </Border>
    </StackPanel>
    <Border Name="B3"
            Width="{Binding Path=ActualWidth,ElementName=stk1}" HorizontalAlignment="Left">
        <TextBlock TextWrapping="NoWrap"
                   Text="testddddddddddddddddddddddddddddddddddd" />
    </Border>
</StackPanel>

Upvotes: 4

Related Questions