开发者

prevent WPF clipping of Grid content when translated vertically

开发者 https://www.devze.com 2023-03-22 00:29 出处:网络
I have the following XAML that displays a stack panel of text blocks. Because of my main grid size, the last few items in the stack panel naturally get cl开发者_运维问答ipped. However if I translate t

I have the following XAML that displays a stack panel of text blocks. Because of my main grid size, the last few items in the stack panel naturally get cl开发者_运维问答ipped. However if I translate the stack panel's parent grid (not the main grid) vertically up, the stack panel's contents still get clipped instead of displaying the items at the bottom. How can I perform the vertical translation on the grid without clipping the bottom contents of the stack panel?

The Viewbox is important - as the first grid needs to size itself to the max height of the main window or monitor.

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow">
    <Viewbox>
        <Grid Width="500" Height="888" Background="#cccccc">
            <Grid Background="#cc99cc">
                <Grid.RenderTransform>
                    <TranslateTransform Y="-800"/>
                </Grid.RenderTransform>
                <StackPanel>
                    <TextBlock Text="This is a test 1" FontSize="50" Foreground="Blue"/>
                    <TextBlock Text="This is a test 2" FontSize="50" Foreground="Red"/>
                    <TextBlock Text="This is a test 3" FontSize="50" Foreground="Green"/>
                    <TextBlock Text="This is a test 4" FontSize="50" Foreground="Orange"/>
                    <TextBlock Text="This is a test 5" FontSize="50" Foreground="Yellow"/>
                    <TextBlock Text="This is a test 6" FontSize="50" Foreground="Purple"/>
                    <TextBlock Text="This is a test 7" FontSize="50" Foreground="Blue"/>
                    <TextBlock Text="This is a test 8" FontSize="50" Foreground="Red"/>
                    <TextBlock Text="This is a test 9" FontSize="50" Foreground="Green"/>
                    <TextBlock Text="This is a test 10" FontSize="50" Foreground="Orange"/>
                    <TextBlock Text="This is a test 11" FontSize="50" Foreground="Yellow"/>
                    <TextBlock Text="This is a test 12" FontSize="50" Foreground="Purple"/>
                    <TextBlock Text="This is a test 13" FontSize="50" Foreground="Blue"/>
                    <TextBlock Text="This is a test 14" FontSize="50" Foreground="Red"/>
                    <TextBlock Text="This is a test 15" FontSize="50" Foreground="Green"/>
                    <TextBlock Text="This is a test 16" FontSize="50" Foreground="Orange"/>
                </StackPanel>
            </Grid>
        </Grid>
    </Viewbox>
</Window>


Just take out the Height property on the Grid.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Wind1"
        Title="MainWindow">
    <Viewbox>
        <Grid Width="500" Background="#cccccc">
            <Grid Background="#cc99cc">
                <Grid.RenderTransform>
                    <TranslateTransform Y="-800"/>
                </Grid.RenderTransform>
                <StackPanel x:Name="stack1">
                    <TextBlock Text="This is a test 1" FontSize="50" Foreground="Blue"/>
                    <TextBlock Text="This is a test 2" FontSize="50" Foreground="Red"/>
                    <TextBlock Text="This is a test 3" FontSize="50" Foreground="Green"/>
                    <TextBlock Text="This is a test 4" FontSize="50" Foreground="Orange"/>
                    <TextBlock Text="This is a test 5" FontSize="50" Foreground="Yellow"/>
                    <TextBlock Text="This is a test 6" FontSize="50" Foreground="Purple"/>
                    <TextBlock Text="This is a test 7" FontSize="50" Foreground="Blue"/>
                    <TextBlock Text="This is a test 8" FontSize="50" Foreground="Red"/>
                    <TextBlock Text="This is a test 9" FontSize="50" Foreground="Green"/>
                    <TextBlock Text="This is a test 10" FontSize="50" Foreground="Orange"/>
                    <TextBlock Text="This is a test 11" FontSize="50" Foreground="Yellow"/>
                    <TextBlock Text="This is a test 12" FontSize="50" Foreground="Purple"/>
                    <TextBlock Text="This is a test 13" FontSize="50" Foreground="Blue"/>
                    <TextBlock Text="This is a test 14" FontSize="50" Foreground="Red"/>
                    <TextBlock Text="This is a test 15" FontSize="50" Foreground="Green"/>
                    <TextBlock Text="This is a test 16" FontSize="50" Foreground="Orange"/>
                </StackPanel>
            </Grid>
        </Grid>
    </Viewbox>
</Window>
0

精彩评论

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

关注公众号