我想在datagrid上使用滚动条。请看下面的代码:
<UserControl x:Class="CustomCopyNas.UserControls.FolderControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Width="700">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ScrollViewer>
<DataGrid Grid.Row="0" ItemsSource="{Binding Folders, Mode=TwoWay}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Path">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox PreviewMouseDown="UIElement_OnPreviewMouseDown" Text="{Binding Path}" IsReadOnly="True"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</Grid>
</UserControl>
我尝试将datagrid包装到ScrollViewer中,但这不起作用。我需要做什么,当我想在我的数据网格中启用scrollViewer时,我该怎么做?
答案 0 :(得分:2)
您可以在ScrollBar
添加DataGrid
而无需添加ScrollViewer
。您可以使用ScrollViewer.HorizontalScrollBarVisibility
和ScrollViewer.VerticalScrollBarVisibility
附加属性显示ScrollBar
:
<DataGrid ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Visible" />
答案 1 :(得分:0)
DataGrid的许多功能都可以自动化,例如滚动。为此,您可以使用 HorizontalScrollBarVisibility 和 VerticalScrollBarVisibility 。
<UserControl x:Class="CustomCopyNas.UserControls.FolderControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Width="700">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<DataGrid HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Row="0" ItemsSource="{Binding Folders, Mode=TwoWay}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Path">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox PreviewMouseDown="UIElement_OnPreviewMouseDown" Text="{Binding Path}" IsReadOnly="True"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>