gpt4 book ai didi

c# - 当鼠标悬停在列标题上时滚动 ListView

转载 作者:行者123 更新时间:2023-11-30 21:27:10 25 4
gpt4 key购买 nike

我有以下 WPF 窗口:

<Window x:Class="WPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="135" Width="150">
<Grid>
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" />
</GridView>
</ListView.View>
<ListViewItem>
<TextBlock Text="Bobby" />
</ListViewItem>
<ListViewItem>
<TextBlock Text="Chuck" />
</ListViewItem>
<ListViewItem>
<TextBlock Text="Frank" />
</ListViewItem>
<ListViewItem>
<TextBlock Text="Sarah" />
</ListViewItem>
<ListViewItem>
<TextBlock Text="Sandra" />
</ListViewItem>
</ListView>
</Grid>
</Window>

我可以使用 ScrollViewer.VerticalScrollBarVisibility="Auto" 为整个 ListView 启用滚动,这是默认行为。但是,当鼠标悬停在列标题(显示文本 Name 的地方)时,这不会启用滚动。即使鼠标悬停在列标题上,我如何允许滚动我的姓名列表?

最佳答案

鼠标悬停在列标题区域没有滚动的原因是 ListView 的控件模板。

标题区域包含另一个 ScrollViewer,它处理 PreviewMouseWheel 隧道事件,防止“主”ScrollViewer 执行此操作。

你有两种方法来解决这个问题。

第一种方式:使用代码直接滚动“主”ScrollViewer

如果您采用这种方式,我建议您创建一个附加属性以使该行为可重用。

public static class AdvancedScrolling
{
// Attached property boilerplate code begin...
public static readonly DependencyProperty EnabledProperty =
DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(AdvancedScrolling), new PropertyMetadata(false, EnabledChanged));

public static void SetEnabled(UIElement element, bool value) => element.SetValue(EnabledProperty, value);

public static bool GetEnabled(UIElement element) => (bool)element.GetValue(EnabledProperty);

// ...Attached property boilerplate code end

private static void EnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// Affects only list views
if (e.NewValue is true && d is ListView listview)
{
// We need the visual tree of the list view, so wait for loading first
listview.Loaded += ListViewLoaded;
}

void ListViewLoaded(object sender, RoutedEventArgs re)
{
listview.Loaded -= ListViewLoaded;

// The default list view (grid view) control template contains
// a Chrome Decorator with a ScrollViewer as Child.
// We need that ScrollViewer.
if ((VisualTreeHelper.GetChild(listview, 0) as Decorator)?.Child is ScrollViewer scrollViewer)
{
// Hooking up the tunneling event
listview.PreviewMouseWheel += (s, me) =>
{
// Always scroll manually
scrollViewer.ScrollToVerticalOffset(
scrollViewer.VerticalOffset
- Math.Sign(me.Delta) * SystemParameters.WheelScrollLines);

// Set Handled to true to prevent the ScrollViewer
// from processing the same event one more time.
me.Handled = true;
};
}
}
}
}

用法:

<ListView local:AdvancedScrolling.Enabled="True">

local 是您的命名空间,例如 xmlns:local="clr-namespace:YourNamespace"

第二种方法是更改​​页眉区域模板

我们只是从那个区域移除滚动查看器,这样它就不会处理鼠标滚轮事件。这将允许我们的“主”ScrollViewer 在鼠标光标位于标题区域上方时对鼠标输入使用react。

我复制了 default control template并从定义标题区域的 DockPanel 中删除了 ScrollViewer

然后你会得到这样的东西:

<ListView>
<ListView.Resources>
<Style x:Key="{x:Static GridView.GridViewScrollViewerStyleKey}" TargetType="ScrollViewer">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ScrollViewer">
<Grid Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<DockPanel Margin="{TemplateBinding Padding}">

<!-- I removed the ScrollViewer here -->

<GridViewHeaderRowPresenter DockPanel.Dock="Top" Margin="2,0,2,0" Columns="{Binding Path=TemplatedParent.View.Columns,
RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderContainerStyle="{Binding
Path=TemplatedParent.View.ColumnHeaderContainerStyle,
RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderTemplate="{Binding
Path=TemplatedParent.View.ColumnHeaderTemplate,
RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderTemplateSelector="{Binding
Path=TemplatedParent.View.ColumnHeaderTemplateSelector,
RelativeSource={RelativeSource TemplatedParent}}" AllowsColumnReorder="{Binding
Path=TemplatedParent.View.AllowsColumnReorder,
RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderContextMenu="{Binding
Path=TemplatedParent.View.ColumnHeaderContextMenu,
RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderToolTip="{Binding
Path=TemplatedParent.View.ColumnHeaderToolTip,
RelativeSource={RelativeSource TemplatedParent}}" SnapsToDevicePixels="{TemplateBinding
SnapsToDevicePixels}" />

<ScrollContentPresenter Name="PART_ScrollContentPresenter"
KeyboardNavigation.DirectionalNavigation="Local" CanContentScroll="True"
CanHorizontallyScroll="False" CanVerticallyScroll="False" />
</DockPanel>

<ScrollBar Name="PART_HorizontalScrollBar" Orientation="Horizontal" Grid.Row="1"
Maximum="{TemplateBinding ScrollableWidth}"
ViewportSize="{TemplateBinding ViewportWidth}"
Value="{TemplateBinding HorizontalOffset}"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" />

<ScrollBar Name="PART_VerticalScrollBar" Grid.Column="1"
Maximum="{TemplateBinding ScrollableHeight}"
ViewportSize="{TemplateBinding ViewportHeight}"
Value="{TemplateBinding VerticalOffset}"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" />

</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.Resources>
</ListView>

关于c# - 当鼠标悬停在列标题上时滚动 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58203240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com