开发者

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

I'm trying to style a TabItem Header, using a path to define the shape of the header.

I'm stuck in a problem that I can't seem to resolve :

If I set the path Stretch property to "None", it won't scale if the text in my TabItem Header is long.

If I set the path Stretch property to "Fill", it will stretch so much that each TabItem Header will be the same width as the TabControl - which mean only one very wide TabItem Header per line...

Do you know a way to stretch the path to the layout (depe开发者_Go百科nding on the TabItemHeader Content), but not more?

I would be very pleased if somebody can help me on this... it's been an annoying while I'm looking for a solution.

Thank you :-)


It's hard when you don't add any sample code but say that your HeaderTemplate looks like below then you can bind the Width of the Path to the ActualWidth 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>


But be aware of rendering performance! Binding to ActualWidth and ActualHeight will result in binding errors as long as the UI hasn't been rendered enterly. And binding errors are expensive...The best way to avoid this, is to set up the binding in code when SizeChanged is called. That's the first moment after measuring and sizing has been finished.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜