Wpf ListView自带的滚动条 ScrollViewer 为什么不能鼠标拖了,只能利用鼠标滚珠滚动
以下是我xaml代码想解决treeview以及listview:
<TreeView Height="{Binding Path=row0}" BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.PanningMode="Both" Background="Transparent" x:Name="tvProperties" Width="260" SelectedItemChanged="tvProperties_SelectedItemChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="-1,20,0,0" ScrollBar.Scroll="TvProperties_Scroll" MouseDown="ScrollViewer_MouseDown" >
<!--<TreeView Height="{Binding Path=row0}" BorderThickness="0" Background="Transparent" x:Name="tvProperties" Width="260" SelectedItemChanged="tvProperties_SelectedItemChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="-1,0,0,0">-->
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded}"></Setter>
<Setter Property="TextBlock.Foreground" Value="White"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<Image VerticalAlignment="Center" Source="{Binding Icon}" Width="16" Height="16" Margin="0,0,2,2"></Image>
<TextBlock VerticalAlignment="Center" Name="nodeName" Text="{Binding DisplayName}"></TextBlock>
<Image VerticalAlignment="Center" Source="{Binding EditIcon}" Margin="2,0,0,0"></Image>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
很明显 是跟ScrollBar.Scroll="TvProperties_Scroll" MouseDown="ScrollViewer_MouseDown"这两个事件有关系
试了一下,鼠标滚轮不会触发TvProperties_Scroll事件,但是鼠标拖动滚动条会,检查一下这个事件做了啥
然后MouseDown="ScrollViewer_MouseDown" 是树的事件,然后你这个名气就很奇怪,你也检查一下吧
我检查了一下,对于之前的ScrollBar.Scroll="TvProperties_Scroll" MouseDown="ScrollViewer_MouseDown"都是我做测试,来测试鼠标拖动滚动条时是否会响应,但我并没有得到和你一样的结论,我无法触发到TvProperties_Scroll事件。
private void TvProperties_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
{
Console.WriteLine("nihao");
}
这是函数的内容,我只想测试鼠标拖动滚动条是否能触发。
那想会不会是哪边截取了这个响应导致TvProperties_Scroll无法接收到响应呢
@Maoey:
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TreeView ItemsSource="{Binding List}" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.PanningMode="Both" Background="Transparent" x:Name="tvProperties" Width="260" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="-1,20,0,0" >
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"></Setter>
<Setter Property="TextBlock.Foreground" Value="White"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate >
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Name="nodeName" Text="{Binding text}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
后台
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List = new List<Temp>();
for (int i = 0; i < 100; i++)
{
List.Add(new Temp());
}
this.DataContext = this;
}
public List<Temp> List { get; set; }
}
public class Temp
{
public string text { get; set; }
public Temp()
{
text = "111";
}
}
你写的莫问题啊,兄弟,看看别的地方,感觉不简单啊
@猝不及防:
<Grid x:Name="gridLeftColumn" Grid.Column="0" Background="#2C3C5C" Margin="0,0,0,-3" >
<Grid.RowDefinitions>
<RowDefinition Height="590*" x:Name="row0"/>
<RowDefinition Height="37.333"/>
<RowDefinition Height="32.667"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Button Content="更新视频树" Style="{StaticResource DefaultButtonStyle}" Click="UpdateTree" Foreground="White" Margin="38,0,10,0" Height="20" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<TreeView Height="{Binding Path=row0}" BorderThickness="0" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.PanningMode="Both" Background="Transparent" x:Name="tvProperties" Width="260" SelectedItemChanged="tvProperties_SelectedItemChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="-1,20,0,0" ScrollBar.Scroll="TvProperties_Scroll" >
<!--<TreeView Height="{Binding Path=row0}" BorderThickness="0" Background="Transparent" x:Name="tvProperties" Width="260" SelectedItemChanged="tvProperties_SelectedItemChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="-1,0,0,0">-->
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded}"></Setter>
<Setter Property="TextBlock.Foreground" Value="White"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<Image VerticalAlignment="Center" Source="{Binding Icon}" Width="16" Height="16" Margin="0,0,2,2"></Image>
<TextBlock VerticalAlignment="Center" Name="nodeName" Text="{Binding DisplayName}"></TextBlock>
<Image VerticalAlignment="Center" Source="{Binding EditIcon}" Margin="2,0,0,0"></Image>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content="起始时间:" FontSize="14" TextBlock.Foreground="White"/>
<StartView:DateTimePicker x:Name="dtStart" FontSize="14" TextBlock.Foreground="White"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content="结束时间:" FontSize="14" TextBlock.Foreground="White"/>
<View:DateTimePicker x:Name="dtEnd" FontSize="14" TextBlock.Foreground="White"/>
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal" FlowDirection="LeftToRight" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button Name="btDownload" Style="{StaticResource buttonDownload}" ToolTip="下载" Click="btDownload_Click" Width="105" Height="36" Margin="0,0,4,0">
</Button>
<Button Name="btDownloadMgr" Style="{StaticResource buttonDownloadMgr}" ToolTip="下载管理" Click="btDownloadMgr_Click" Width="105" Height="36" >
</Button>
</StackPanel>
</Grid>
这是这个是整个的gird的xaml,谢谢你简洁的代码。我再检查一下
最终发现问题,由于拖动界面函数把鼠标点击滑轮的响应给获取了,添加了无响应区后就好了