WPF保持控制不影响滚动查看器宽度?

时间:2013-07-31 08:45:59

标签: wpf layout scrollviewer

有没有办法阻止包装面板(或其他控件)参与滚动查看器宽度计算?例如下面,我希望包装面板保持在其他控件创建的宽度范围内,但不会直接影响宽度计算。 (即)我喜欢类似于Auto关闭的行为,但仍然允许其他内容的水平滚动变宽。

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <ScrollViewer HorizontalScrollBarVisibility="Auto">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <WrapPanel Grid.Column="1" Name="ctlWrap" />
            <TextBox Grid.Row="1" Grid.Column="1" Width="100" HorizontalAlignment="Left" Name="ctlText" />
            <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal">
                <Button Click="Button_Click">Add Wrap</Button>
                <Button Click="Button_Click_1">Remove Wrap</Button>
                <Button Click="Button_Click_2">Add Text</Button>
                <Button Click="Button_Click_3">Remove Text</Button>
            </StackPanel>
        </Grid>
    </ScrollViewer>
</Grid>
</Window>

这是我用于按钮的代码:

Class MainWindow 

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    ctlWrap.Children.Add(New Button With {.Content = "Button " & ctlWrap.Children.Count + 1})
End Sub

Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
    If ctlWrap.Children.Count Then
        ctlWrap.Children.RemoveAt(ctlWrap.Children.Count - 1)
    End If
End Sub

Private Sub Button_Click_2(sender As Object, e As RoutedEventArgs)
    ctlText.Width += 30
End Sub

Private Sub Button_Click_3(sender As Object, e As RoutedEventArgs)
    If ctlText.Width > 60 Then ctlText.Width -= 30
End Sub
End Class

1 个答案:

答案 0 :(得分:2)

我略微投降了。我将按钮放在网格的左列中,然后将宽度和最大宽度绑定到滚动条的视口宽度,将左边距限制为水平偏移。

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:app="clr-namespace:WpfApplication14"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <app:MarginConverter x:Key="mc" />
</Window.Resources>
<Grid>
    <ScrollViewer HorizontalScrollBarVisibility="Auto" Name="ctlScroll">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <WrapPanel Grid.ColumnSpan="2" Grid.Column="0" Name="ctlWrap" Width="{Binding ViewportWidth, ElementName=ctlScroll}" MaxWidth="{Binding ViewportWidth, ElementName=ctlScroll}" Margin="{Binding HorizontalOffset, ElementName=ctlScroll, Converter={StaticResource mc}}" HorizontalAlignment="Left" />
            <TextBox Grid.Row="1" Grid.Column="1" Width="100" HorizontalAlignment="Left" Name="ctlText" />
            <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal">
                <Button Click="Button_Click">Add Wrap</Button>
                <Button Click="Button_Click_1">Remove Wrap</Button>
                <Button Click="Button_Click_2">Add Text</Button>
                <Button Click="Button_Click_3">Remove Text</Button>
            </StackPanel>
        </Grid>
    </ScrollViewer>
</Grid>
</Window>

保证金转换器来自here