WPF在扩展器中滚动数据网格

时间:2012-10-18 08:00:39

标签: c# wpf ribbon

我在扩展器中有一个数据网格,而gatagrid有超过10列。然后在扩展器中的datagrid
应该滚动。但是无法滚动查看其他列。我的代码如下。

                <Expander Header="Attribute" VerticalAlignment="Top" Grid.Row="1" >
                    <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Visible" CanContentScroll="True" Grid.Row="1" >
                           <DataGrid DataContext="{Binding}" Height="257" ItemsSource="{Binding}"  MinHeight="200" Name="gridview1" Grid.Row="2" Grid.Column="0" 
                           VerticalAlignment="Bottom" IsReadOnly="True"></DataGrid>
                     </ScrollViewer>
                </Expander>

1 个答案:

答案 0 :(得分:2)

DataGrid在其ControlTemplate中嵌入了ScrollViewer。

所以你可以做两件事:

如果您真的想使用自己的ScrollViewer(但请注意,通过这样做,您将失去DataGrid虚拟化等功能)此代码应该有效:

<Expander Header="Attribute" VerticalAlignment="Top" Grid.Row="1" >
    <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" CanContentScroll="False" Grid.Row="1" >
        <DataGrid Height="257"  MinHeight="200" Name="gridview1" Grid.Row="2" Grid.Column="0" 
                       VerticalAlignment="Bottom" IsReadOnly="True">
            <DataGrid.Style>
                <Style>
                    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
                    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled" />
                </Style>
            </DataGrid.Style>
        </DataGrid>
    </ScrollViewer>
</Expander>

但是,我建议你这样做:

<Expander Header="Attribute" VerticalAlignment="Top" Grid.Row="1" >
        <DataGrid Height="257"  MinHeight="200" Name="gridview1" Grid.Row="2" Grid.Column="0" VerticalAlignment="Bottom" IsReadOnly="True">
            <DataGrid.Style>
                <Style>
                    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Visible" />
                    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Visible" />
                </Style>
            </DataGrid.Style>
    </DataGrid>
</Expander>

甚至不需要DataGrid.Style。