WPF: Is it possible to adapt the path size to the size of the layout, but still stretching it?

I am trying to create a TabItem header using a path to determine the shape of the header.

I am stuck in a problem that I cannot solve:

If I set the path's Stretch property to "None", it will not scale if the text in the TabItem header is long.

If I set the pathโ€™s Stretch property to Fill, it will stretch so much that each TabItem will be the same width as TabControl, which means only one very wide TabItem in a row ...

Do you know a way to stretch the path to the layout (depending on the contents of the TabItemHeader), but no more?

I would be very pleased if someone can help me with this ... it was unpleasant when I was looking for a solution.

Thank:-)

+3
source share
2 answers

Itโ€™s tough if you donโ€™t add any sample code, but say that your HeaderTemplate looks lower, you can bind the path width to the actual width of the TextBlock.

<TabItem.HeaderTemplate>
    <DataTemplate>
        <Border x:Name="grid">
            <Grid>
                <Path Data="..."
                      Stretch="Fill"
                      Width="{Binding ElementName=grid, Path=ActualWidth}" />
                <TextBlock Name="textBlock"
                           Margin="4"
                           FontSize="15"
                           Text="{Binding}"/>                                
            </Grid>
        </Border>
    </DataTemplate>
</TabItem.HeaderTemplate>
+5
source

But keep in mind rendering performance! Binding to ActualWidth and ActualHeight will result in binding errors if the user interface is not displayed inaccurately. And binding errors are expensive ... The best way to avoid this is to set the binding in the code when calling SizeChanged. This is the first moment after measurement and calibration.

+1
source

Source: https://habr.com/ru/post/1774782/


All Articles