为什么我的ScrollViewer毁了我的网格布局? WPF [英] Why does my ScrollViewer destroy my Grid Layout? WPF

查看:157
本文介绍了为什么我的ScrollViewer毁了我的网格布局? WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:当添加一个ScrollViwer围绕网格网格比例被取消

Problem: When adding a ScrollViwer around a Grid the Grid scaling is cancelled!

Eksampel:我创建了一个网格
宽3列,1 。coulymn应该总是比列2和3大2倍!
没有的ScrollViewer这是总是正确的,但添加后它允许每列来决定自己的大小。

Eksampel: I have created a grid width 3 columns, the 1. coulymn should always be 2 times larger than column 2 and 3! Without the ScrollViewer this is always true, but when adding it it allows each column to decide its own size.

<Window x:Class="alternatingGridRow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="200" Width="Auto" Loaded="WindowLoaded">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
    <Grid x:Name="LayoutRoot" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MinHeight="23" MaxHeight="60"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
            <TextBlock HorizontalAlignment="Stretch" Text="sdasdasdasdsadsadasddasdck" TextWrapping="Wrap" VerticalAlignment="Top" />
            <TextBlock Foreground="Red" Grid.Column="1" HorizontalAlignment="Stretch" Text="sdasdasdasdsadsadasddasdck" TextWrapping="Wrap" VerticalAlignment="Top" />
    </Grid>
</ScrollViewer>



你可以清楚地看到比例因子是完全错误的!由于2列的方式来大!和3列是一些随机的大小...

As you can clearly see the scaling factors are completely wrong! As the 2. column is way to large! and the 3. column is some random size...

错误缩放因素

在这个任何意见很好收到....
干杯马丁

Any advice on this is well recieved.... Cheers Martin

推荐答案

好吧,我看到了为什么列大小一拧。结果
,而你的观点。我想到了一个解决方案,因为我看了你的帖子...

Ok I see your point in why the column sizes a screwed.
But.. I thought of a solution as I read your posts...

由于穆罕默德说,设置在我的网格固定宽度,以及..我希望我的网格有相同的宽度的ScrollViewer除非得到小,那么我想的ScrollViewer生效!
所以..我的解决办法是:

As, Mohammed said, set a fixed width on my grid, well.. I want my grid to have same width as scrollviewer unless it gets to small, then I want the scrollviewer to take affect! So.. my solution is:

MinWidth="500" Width="{Binding ActualWidth, ElementName=scrollviewer}"

<Window x:Class="alternatingGridRow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="200" Width="Auto">
<ScrollViewer x:Name="scrollviewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
    <Grid x:Name="LayoutRoot" ShowGridLines="True" MinWidth="500" Width="{Binding ActualWidth, ElementName=scrollviewer}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MinHeight="23" MaxHeight="60"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
            <TextBlock HorizontalAlignment="Stretch" Text="sdasdasdasdsadsadasddasdck" TextWrapping="Wrap" VerticalAlignment="Top" />
            <TextBlock Foreground="Red" Grid.Column="1" HorizontalAlignment="Stretch" Text="sdasdasdasdsadsadasddasdck" TextWrapping="Wrap" VerticalAlignment="Top" />
    </Grid>
</ScrollViewer>

</Window>



(仅固定的水平)

(Only fixed for horizontal)

THX。

这篇关于为什么我的ScrollViewer毁了我的网格布局? WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆