WPF ScrollViewer:水平滚动不起作用

时间:2010-10-09 05:26:35

标签: wpf scrollviewer

为什么这不支持水平滚动?

<ScrollViewer>
    <ItemsControl ItemsSource="{Binding Territories}" HorizontalAlignment="Left">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel CanHorizontallyScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Auto" >

                </VirtualizingStackPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="75" />
                        <ColumnDefinition Width="75" />
                        <ColumnDefinition Width="150" />
                        <ColumnDefinition Width="150" />
                        <ColumnDefinition Width="65" />
                        <ColumnDefinition Width="60" />
                        <ColumnDefinition Width="75" />
                        <ColumnDefinition Width="75" />
                    </Grid.ColumnDefinitions>

                    <TextBox Grid.Row="0" Grid.Column="0" Text="{Binding Quantity, StringFormat=N0, ValidatesOnDataErrors=True}" />
                    <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding ProductionNumber, ValidatesOnDataErrors=True}" />

                    <ComboBox Grid.Row="0" Grid.Column="2" SelectedValuePath="PrimaryKey" SelectedValue="{Binding RepKey}" ItemsSource="{Binding RepCanidates}" TextSearch.TextPath="FullName" Margin="4">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding FullName}" ToolTip="{Binding FullName}" Width="150" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>

                        <ComboBox.ItemContainerStyle>
                            <Style TargetType="{x:Type ComboBoxItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                                            <Border x:Name="Bd" 
                                                                BorderBrush="{TemplateBinding BorderBrush}" 
                                                                BorderThickness="{TemplateBinding BorderThickness}" 
                                                                Background="{TemplateBinding Background}">
                                                <StackPanel Orientation="Horizontal">
                                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"  
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                    <TextBlock Text="{Binding CompanyBranchName}" ToolTip="{Binding CompanyBranchName}" Width="200"/>
                                                    <TextBlock Margin="4,0,0,0" Text="{Binding ShortAddress}" ToolTip="{Binding ShortAddress}" />
                                                </StackPanel>
                                            </Border>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsHighlighted" Value="True">
                                                    <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                                                </Trigger>
                                                <Trigger Property="IsEnabled" Value="False">
                                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ComboBox.ItemContainerStyle>

                    </ComboBox>

                    <Button Grid.Row="1" Grid.Column="2" Content="Advanced Search"  Name="cmdAdvancedSearch" Click="cmdAdvancedSearch_Click"  />

                    <ComboBox Grid.Row="0" Grid.Column="3" SelectedValuePath="PrimaryKey" SelectedValue="{Binding CompanyBranchKey}" ItemsSource="{Binding CompanyBranchCanidates}" Margin="4" IsEnabled="{Binding CompanyBranchKeyEditable}">

                    </ComboBox>

                    <TextBox Grid.Row="0" Grid.Column="4" Text="{Binding ZipCode}" IsEnabled="{Binding ZipCodeEditable}"/>

                    <ComboBox Grid.Row="0" Grid.Column="5" SelectedValuePath="PrimaryKey" SelectedValue="{Binding StateKey}" ItemsSource="{Binding StateCanidates}" Margin="4" IsEnabled="{Binding StateKeyEditable}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding State}" ToolTip="{Binding StateName}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>

                    </ComboBox>

                    <ComboBox Grid.Row="0" Grid.Column="6" SelectedValuePath="PrimaryKey" SelectedValue="{Binding TerritoryKey, ValidatesOnDataErrors=True}" ItemsSource="{Binding TerritoryCanidates}" Margin="4" IsEnabled="{Binding TerritoryKeyEditable}">

                    </ComboBox>

                    <Button  Grid.Row="0" Grid.Column="7" Content="Delete" Click="cmdDeleteRow_Click" Margin="4" />

                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

1 个答案:

答案 0 :(得分:24)

此更改将起作用:

<ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Visible">