开发者

How to make a child element limited in size by its parent in WPF?

I have a StackPanel which is larger than its parent Grid. I confirmed that using Snoop WPF Spy. The Widths are set to default. How to constrain it?

I don't like the ViewBox solution because it zooms out my text, and I want it to be wrapped/trimmed.

Edit the XAML as requested

<Grid>
    <StackPanel
        Orientation="Horizontal">
        <Border
            BorderBrush="Black"
            BorderThickness="1"
            Width="{ Binding BorderScreenShot }"
            Margin="0,-8,0,0">
            <Image
                ToolTip="Some Text"
                Cursor="Hand"
                Source="{ Binding Image }"
                Stretch="Fill"
                Visibility="{ Binding ImageVisibility }"
                MouseLeftButtonUp="Image_MouseLeftButtonUp" />
        </Border&g开发者_运维百科t;
        <StackPanel
            Orientation="Vertical"
            Margin="10,0,0,0">
            <TextBlock
                Text="{ Binding Name }"
                TextWrapping="Wrap" />
            <TextBlock
                Text="{ Binding LongText }"
                TextWrapping="Wrap" />
            <StackPanel
                Orientation="Horizontal">
                <TextBlock
                    Text="{ Binding Category }" />
                <TextBlock
                    Text="{ Binding Version }" />
            </StackPanel>
        </StackPanel>
    </StackPanel>
</Grid>


You could try changing the StackPanel to a DockPanel (with appropriate DockPanel.Dock properties on the children).

That will mimic the effect of the StackPanel but allow the children to be constrained.

Or, use a WrapPanel instead of a StackPanel.

StackPanels have infinite client size, so nothing inside a StackPanel will wrap or trim without explicit dimensions being set (instead, the StackPanel just grows to fit the contents).


It's not possible for a child element to be larger than it's parent element, unless it's content is larger. If you want to trim your text you could use TextBlock.TextTrimming = WordEllipsis/CharacterEllipsis. This would generate the desired behaviour if my interpretation of the question is correct.

However, this does not work properly with a StackPanel. As the other answer suggests, you could use a DockPanel or Grid to arrange the content.

Alternatively you could wrap your StackPanel in a ScrollViewer and use a scrollbar to handle the overflow.

Update This should fix it

    <DockPanel>
    <Border    
        DockPanel.Dock="Left"
        BorderBrush="Black"            
        BorderThickness="1"            
        Width="{ Binding BorderScreenShot }"           
        Margin="0,-8,0,0">
        <Image                ToolTip="Some Text"                Cursor="Hand"                Source="{ Binding Image }"                Stretch="Fill"                Visibility="{ Binding ImageVisibility }"                MouseLeftButtonUp="Image_MouseLeftButtonUp" />
    </Border>
    <StackPanel            
            DockPanel.Dock="Bottom"
            Orientation="Horizontal">
        <TextBlock                    Text="{ Binding Category }" />
        <TextBlock                    Text="{ Binding Version }" />
    </StackPanel>
    <TextBlock             
        DockPanel.Dock="Top"
        Text="{ Binding Name }"                
        TextWrapping="Wrap" />
    <TextBlock                
        Text="{ Binding LongText }"                
        TextTrimming="WordEllipsis"
        TextWrapping="Wrap" />
</DockPanel>


If you don't want to shrink the content, there really isn't any other way to constrain it - especially since images are involved.

That pretty much leaves you with the ScrollViewer, which was designed for just this scenario. Personally, I like combining the ability to zoom in/out with the ScrollViewer functionality. It gives your users the most options in how they look at your content.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜