如何使子元素在WPF中的父元素限制?

Jad*_*ias 6 .net wpf inheritance width

我有一个StackPanel比它的父母大的Grid.我确认使用Snoop WPF间谍.该Widths的设置为默认值.如何约束呢?

我不喜欢这个ViewBox解决方案,因为它缩小了我的文字,我希望它被包装/修剪.

根据请求编辑 XAML

<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>
        <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>
Run Code Online (Sandbox Code Playgroud)

Mar*_*rcE 6

您可以尝试将其更改StackPanel为a DockPanel(对子项具有适当的DockPanel.Dock属性).

这将模仿StackPanel的效果,但允许孩子受到约束.

或者,使用WrapPanel而不是StackPanel.

StackPanels具有无限的客户端大小,因此如果没有设置显式维度,StackPanel内部的任何内容都不会包装或修剪(相反,StackPanel只会增长以适应内容).