Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGrid ScrollViewer不工作_C#_Wpf_Xaml_Visual Studio 2013_Scrollviewer - Fatal编程技术网

C# DataGrid ScrollViewer不工作

C# DataGrid ScrollViewer不工作,c#,wpf,xaml,visual-studio-2013,scrollviewer,C#,Wpf,Xaml,Visual Studio 2013,Scrollviewer,无法使用键盘箭头键滚动数据网格中的数据。 ScrollViewer\u KeyDown事件在与Grid一起使用时工作正常,但在与datagrid defaultScrollViewer一起使用时未获得调用 <DataGrid.Resources> <Style TargetType="ScrollViewer"> <Setter Property="VerticalScrollBarVisibility" Value="Auto"/&g

无法使用键盘箭头键滚动数据网格中的数据。
ScrollViewer\u KeyDown
事件在与
Grid
一起使用时工作正常,但在与datagrid default
ScrollViewer
一起使用时未获得调用

<DataGrid.Resources>
     <Style TargetType="ScrollViewer">
          <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>                
          <EventSetter Event="KeyDown" Handler="ScrollViewer_KeyDown"></EventSetter>
     </Style>
</DataGrid.Resources>       
这项工作:

<Grid FocusManager.FocusedElement="{Binding ElementName=MyScrollViewer}">
    <ScrollViewer x:Name="MyScrollViewer" VerticalScrollBarVisibility="Auto">            
        <DataGrid ItemsSource="{Binding MyCollection}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        </DataGrid>
    </ScrollViewer>
</Grid>


不需要处理任何事件。关键是通过设置FocusManager.FocusedElement来确保scrollviewer具有焦点。

使用
PreviewKeyDown
事件。我相信
DataGrid
会处理
KeyDown
事件以更改行的选择(如果按了向上/向下箭头)。谢谢,但它不起作用。是否执行了处理程序?您是否尝试在其中设置断点?尝试使用断点。处理程序未被执行。是否有其他东西处理该事件?实际上,我想显示DataGrid中的滚动条。您建议的方式工作正常,但是它在DataGrid外部显示滚动条。您可以使用DataGrid ScrollViewer(您最初使用的方式),然后确保DataGrid具有焦点。唯一需要注意的是,当一行获得焦点时,向下箭头单击将向下移动一行。
<Grid FocusManager.FocusedElement="{Binding ElementName=MyScrollViewer}">
    <ScrollViewer x:Name="MyScrollViewer" VerticalScrollBarVisibility="Auto">            
        <DataGrid ItemsSource="{Binding MyCollection}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        </DataGrid>
    </ScrollViewer>
</Grid>