簡體   English   中英

使用 ctrl 按鈕在數據網格上滾動

[英]Scrolling on a datagrid with ctrl button

我希望我的數據網格僅在您同時按下 ctrl 鍵時使用鼠標滾輪滾動,否則我想滾動頁面。 我是 wpf 和 mvvm 的新手,這是我在應用程序中使用的模式。

這是包含數據網格的頁面:

<Page x:Class="Projectname.ChapterPage"
      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" 
      xmlns:local="clr-namespace:Projectname" 
      xmlns:core="clr-namespace:Projectname.Core;assembly=Projectname.Core"
      mc:Ignorable="d" 
      d:DesignHeight="700" d:DesignWidth="900"
      Title="ChapterPage">

    <StackPanel>

        <TextBlock Grid.Column="0"
                           Grid.ColumnSpan="2"
                           Text="Kapitel"
                           Style="{StaticResource HeaderText}" 
                           Foreground="{StaticResource 
                           ForegroundVeryLightBrush}"
                           FontSize="{StaticResource FontSizeLarge}"
                           FontWeight="Bold"
                           Margin="50 -45 50 15"/>

        <Border Background="{StaticResource BlueBrush}"
                CornerRadius="10"
                Margin="10"
                MinHeight="45"
                Padding="2">

            <StackPanel HorizontalAlignment="Center"
                      Orientation="Horizontal">

                <Button Command="{Binding SaveCommand}"
                    Style="{StaticResource IconGrowButton}"
                    ToolTip="Speichern"
                    Margin="10 0"
                    MaxHeight="30"
                    MinWidth="30"
                    Cursor="Hand">
                    <Image Source="../Images/Icons/Save.png" />
                </Button>

                <Button Command="{Binding DeleteCommand}"
                    Style="{StaticResource IconGrowButton}"
                    ToolTip="Löschen"
                    MaxHeight="30"
                    MinWidth="30"
                    Margin="10 0 80 0"
                    Cursor="Hand">
                    <Image Source="../Images/Icons/Delete.png" />
                </Button>

                <Button Command="{Binding ResetCommand}"
                    Style="{StaticResource IconGrowButton}"
                    ToolTip="Zurücksetzen"
                    MaxHeight="30"
                    MinWidth="30"
                    Margin="10 0"
                    Cursor="Hand">
                    <Image Source="../Images/Icons/Reset.png" />
                </Button>

            </StackPanel>

        </Border>

        <DataGrid Margin="15 10"
                  HorizontalAlignment="Stretch"
                  ItemsSource="{Binding Chapters, 
                      UpdateSourceTrigger=PropertyChanged}"
                  Visibility="{Binding ChaptersVisibility, 
                      Converter={local:BooleanToVisibilityConverter}}"
                  SelectedItem="{Binding SelectedChapter}"/>

    </StackPanel>
</Page>

我的頁面被加載到一個窗口中,滾動內容的滾動查看器在窗口中。

那是窗口:

        <Grid>

            <StackPanel>

                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <Border Background="{StaticResource RedBrush}">
                        <Button Command="{Binding NavMenuCommand}"
                            Style="{StaticResource SystemIconButton}"
                            Cursor="Hand"
                            HorizontalAlignment="Left">
                            <Button.Content>
                                <Image Source="../Images/Icons/burger- 
                                    menue.png"
                                Height="25"/>
                            </Button.Content>
                        </Button>
                    </Border>

                </Grid>

                <local:ChapterPage
                       Padding="8 0"/>

            </StackPanel>

            <local:NavMenu Margin="0,41,628,0"/>

        </Grid>
    </ScrollViewer>

我完全不知道如何實現這一點,但如果可以避免的話,我不想使用 xaml.cs。

我想要不同的滾動行為的原因是因為數據網格和標題區域之間有一個很大的用戶控件。 這是頁面划分的圖片。

您需要通過兩個或所有參與ScrollViewer的公共父級來處理PreviewMouseWheel事件:

主窗口.xaml
簡化視圖:

<Window PreviewMouseWheel="HandleScroll_OnPreviewMouseWheel">
  <ScrollViewer x:Name="RootScrollViewer">
    <DataGrid x:Name="DataGridToScrollOnKeyPress" />
  </ScrollViewer>
</Window>

主窗口.xaml.cs

private ScrollViewer DataGridScrollViewer { get; set; }

private void OnLoaded(object sender, RoutedEventArgs e)
{
  // Find DataGrid by name (in case of multiple DataGrids)
  if (TryFindVisualChildElement(this.Root, "DataGridToScrollOnKeyPress", out DataGrid dataGrid))
  { 
    // And get its ScrollViewer (by type)
    if (TryFindVisualChildElement(dataGrid, string.Empty, out ScrollViewer scrollViewer))
    {
      this.DataGridScrollViewer = scrollViewer;
    }
  }
}

private void HandleScroll_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
  // Give the main scroll viewer chance to handle its own scroll,
  // if it is the scroll source (preserve default behavior)
  if (e.Source == this.Root 
      && !Keyboard.IsKeyDown(Key.LeftCtrl) 
      && !Keyboard.IsKeyDown(Key.RightCtrl))
  {
    return;
  }

  e.Handled = true;
  if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  {
    if (e.Delta < 0)
    {
      this.DataGridScrollViewer?.LineDown();
    }
    else
    {
      this.DataGridScrollViewer?.LineUp();
    }
  }
  else // Some other control is scroll source => scroll main scroll viewer
  {
    if (e.Delta < 0)
    {
      this.RootScrollViewer.LineDown();
    }
    else
    {
      this.RootScrollViewer.LineUp();
    }
  }
}

// If the 'childElementName' parameter is NULL, an empty string or whitespace it will be ignored (only the type will be relevant)
public bool TryFindVisualChildElement<TChild>(DependencyObject parent, string childElementName, out TChild childElement)
  where TChild : FrameworkElement
{
  childElement = null;
  if (parent == null)
  {
    return false;
  }

  for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
  {
    DependencyObject child = VisualTreeHelper.GetChild(parent, i);
    if (child is TChild resultElement 
        && (string.IsNullOrWhiteSpace(childElementName) || resultElement.Name.Equals(childElementName, StringComparison.Ordinal)))
    {
      childElement = resultElement;
      return true;
    }

    if (TryFindVisualChildElement(child, childElementName, out childElement))
    {
      return true;
    }
  }

  return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM