WPF ListBox 滚动条的问题

love_study 2012-11-21 03:11:00
请问在WPF 下,我用ListBox控件显示一排图片,我对ListBox进行了横向的显示,只显示 横向滚动条
我在ListBox中设置
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>

图片绑定和显示,但是发现,横向滚动条不能跟随鼠标滚轮进行滚动了,控件貌似还是默认的纵向滚动条跟随着鼠标滚轮,请问这块怎么修改
...全文
1528 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
宇峰科技 2013-11-15
  • 打赏
  • 举报
回复
好像是要像楼上那样的方法来解决
小猪八Q 2013-11-13
  • 打赏
  • 举报
回复
引用 14 楼 xiaoxiong345064855 的回复:
[quote=引用 13 楼 xzw_email 的回复:] 在ListBox的PreviewMouseWheel事件中做如下处理: private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { ScrollViewer scrollViewer = null; ItemsControl depObj = (ItemsControl)sender; scrollViewer = FindVisualChild<ScrollViewer>(depObj); if (scrollViewer != null) { scrollViewer.LineRight(); } } public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { return (T)child; } T childItem = FindVisualChild<T>(child); if (childItem != null) return childItem; } } return null; }
谢谢你,这个问题我找了好久[/quote]
xiaoxiong345 2013-09-05
  • 打赏
  • 举报
回复
引用 13 楼 xzw_email 的回复:
在ListBox的PreviewMouseWheel事件中做如下处理: private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { ScrollViewer scrollViewer = null; ItemsControl depObj = (ItemsControl)sender; scrollViewer = FindVisualChild<ScrollViewer>(depObj); if (scrollViewer != null) { scrollViewer.LineRight(); } } public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { return (T)child; } T childItem = FindVisualChild<T>(child); if (childItem != null) return childItem; } } return null; }
谢谢你,这个问题我找了好久
格调110 2012-12-01
  • 打赏
  • 举报
回复
在ListBox的PreviewMouseWheel事件中做如下处理: private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { ScrollViewer scrollViewer = null; ItemsControl depObj = (ItemsControl)sender; scrollViewer = FindVisualChild<ScrollViewer>(depObj); if (scrollViewer != null) { scrollViewer.LineRight(); } } public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { return (T)child; } T childItem = FindVisualChild<T>(child); if (childItem != null) return childItem; } } return null; }
love_study 2012-11-30
  • 打赏
  • 举报
回复
谢谢楼上,您这是修改的listbox 滚动条的样式,这块并不是我问的问题,我的问题时,横向滚动条随鼠标滚轮滚动
在路上20130607 2012-11-26
  • 打赏
  • 举报
回复
下面是调用

 <ListBox    Style="{StaticResource listBoxWithAutoScroll_Horizontal}" >
  </ListBox> 
在路上20130607 2012-11-26
  • 打赏
  • 举报
回复
和上面的在一个文件中

  <!-- Height="{Binding Height, ElementName=Panel}"-->
    <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#CCC" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="HorizontalNormalBrush" StartPoint="0,0" EndPoint="1,0">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#CCC" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="LightBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#EEE" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="HorizontalLightBrush" StartPoint="0,0" EndPoint="1,0">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#EEE" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="DarkBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#AAA" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="PressedBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#BBB" Offset="0.0"/>
                <GradientStop Color="#EEE" Offset="0.1"/>
                <GradientStop Color="#EEE" Offset="0.9"/>
                <GradientStop Color="#FFF" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />

    <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />

    <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />

    <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />

    <!-- Border Brushes -->

    <LinearGradientBrush x:Key="NormalBorderBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#CCC" Offset="0.0"/>
                <GradientStop Color="#444" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="HorizontalNormalBorderBrush" StartPoint="0,0" EndPoint="1,0">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#CCC" Offset="0.0"/>
                <GradientStop Color="#444" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="DefaultedBorderBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#777" Offset="0.0"/>
                <GradientStop Color="#000" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="PressedBorderBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#444" Offset="0.0"/>
                <GradientStop Color="#888" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

    <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />

    <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />

    <SolidColorBrush x:Key="LightBorderBrush" Color="#AAA" />

    <!-- Miscellaneous Brushes -->

    <SolidColorBrush x:Key="LightColorBrush" Color="#DDD" />

    <Style x:Key="ScrollBarLineButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}"><!---->
                    <Border Name="Border" CornerRadius="2"  Background="{StaticResource NormalBrush}"
                              BorderBrush="{StaticResource NormalBorderBrush}" >
                        <Path HorizontalAlignment="Center" VerticalAlignment="Center" Fill="Gray"
                                Data="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" 
                                Opacity="{Binding Path=Opacity, RelativeSource={RelativeSource TemplatedParent}}"  />
                        <!--<Border.Background>
                            <ImageBrush ImageSource="Images/nav.jpg"></ImageBrush>
                        </Border.Background>-->
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ScrollBarPageButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Border Background="Transparent" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

在路上20130607 2012-11-26
  • 打赏
  • 举报
回复

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  
    <Style x:Key="ScrollBarThumb" TargetType="{x:Type Thumb}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border CornerRadius="2" Background="{TemplateBinding Background}" 
                                BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


    <!--listBoxWithAutoScroll_Horizontal-->
    <Style x:Key="listBoxWithAutoScroll_Horizontal" TargetType="{x:Type ListBox}" >
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value >
                <ControlTemplate>
                    <Grid>
                        <ScrollViewer x:Name="scrollviewer" >
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}" >
                                    <Grid>
                                        <ScrollBar x:Name="PART_HorizontalScrollBar" Orientation="Horizontal"
                                                               Value="{TemplateBinding HorizontalOffset}"
                                                               Maximum="{TemplateBinding ScrollableWidth}"
                                                               ViewportSize="{TemplateBinding ViewportWidth}"
															   VerticalAlignment="Bottom"
                                                               Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                                               Height="5">
                                            <ScrollBar.Template>
                                                <ControlTemplate>
                                                    <Grid Background="{TemplateBinding Background}" Height="10" >
                                                        <Track x:Name="PART_Track">
                                                            <Track.DecreaseRepeatButton>
                                                                <RepeatButton Command="ScrollBar.PageLeftCommand"
						                                                    Style="{StaticResource ScrollBarPageButton}"/>
                                                            </Track.DecreaseRepeatButton>
                                                            <Track.IncreaseRepeatButton>
                                                                <RepeatButton Command="ScrollBar.PageRightCommand"
						                                                    Style="{StaticResource ScrollBarPageButton}"/>
                                                            </Track.IncreaseRepeatButton>
                                                            <Track.Thumb>
                                                                <Thumb Style="{StaticResource ScrollBarThumb}" 
                                                                         Background="Gray" Opacity="0.5" />
                                                            </Track.Thumb>
                                                        </Track>
                                                    </Grid>
                                                </ControlTemplate>
                                            </ScrollBar.Template>
                                        </ScrollBar>
                                        <ScrollContentPresenter Margin="0,4" Height="Auto" VerticalAlignment="Center"/>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter/>
                        </ScrollViewer>
                        <Grid x:Name="Panel" Margin="0,1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <RepeatButton x:Name="LineLeftButton" Grid.Column="0" Width="30" Height="100" Opacity="0"
                                      Style="{StaticResource ScrollBarLineButton}"
                                      Content="M 8 0 L 8 32 L 0 16 Z"       
                                      Command="{x:Static ScrollBar.LineLeftCommand}"       
                                      CommandTarget="{Binding ElementName=scrollviewer}"
                                      ClickMode="Hover" >
                                <!--<RepeatButton.Background>
                                    <ImageBrush  ImageSource="Images/nav.jpg"></ImageBrush>
                                </RepeatButton.Background>-->
                            </RepeatButton>
                            <RepeatButton x:Name="LineRightButton" Grid.Column="2"  Width="30" Height="100" Opacity="0"
                                      Style="{StaticResource ScrollBarLineButton}"
                                      Content="M 0 0 L 8 16 L 0 32 Z" 
                                      Command="{x:Static ScrollBar.LineRightCommand}"       
                                      CommandTarget="{Binding ElementName=scrollviewer}"
                                      ClickMode="Hover">
                                <!--<RepeatButton.Background>
                                    <ImageBrush  ImageSource="Images/nav.jpg"></ImageBrush>
                                </RepeatButton.Background>-->
                            </RepeatButton>
                        </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True"/>
                                <Condition Property="ComputedHorizontalScrollBarVisibility" SourceName="scrollviewer" Value="Visible"/>
                            </MultiTrigger.Conditions>
                            <!--<Setter TargetName="LineLeftButton" Property="Visibility" Value="Visible"/>
                                <Setter TargetName="LineRightButton" Property="Visibility" Value="Visible"/>-->
                            <MultiTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="LineLeftButton" 
                                                             Storyboard.TargetProperty="Opacity" To="0.8" Duration="0:0:0.25"/>
                                        <DoubleAnimation Storyboard.TargetName="LineRightButton" 
                                                             Storyboard.TargetProperty="Opacity" To="0.8" Duration="0:0:0.25"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </MultiTrigger.EnterActions>
                            <MultiTrigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="LineLeftButton" 
                                                             Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.25"/>
                                        <DoubleAnimation Storyboard.TargetName="LineRightButton" 
                                                             Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.25"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </MultiTrigger.ExitActions>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemsPanel">
            <Setter.Value >
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
love_study 2012-11-26
  • 打赏
  • 举报
回复
难道此贴要沉吗?
在路上20130607 2012-11-22
  • 打赏
  • 举报
回复
滚轮事件 应该还是控制上下滚动的 不清楚有没有设置 设置控制成左右的 如果没有 可以自己在 滚轮事件 里面加处理
love_study 2012-11-22
  • 打赏
  • 举报
回复
横向显示很简单!我的问题是:ListBox横向显示后,在只显示横向滚动条的情况下,如何让横向滚动条随鼠标滚轮滚动
sky2008444 2012-11-22
  • 打赏
  • 举报
回复
哦 我知道了 你想要做的是将图片水平排列到ListBox里面是吧 这个需要修改ListBox的ItemsPanel样式,你可以这样写:

<ListBox>
 <ListBox.ItemsPanel>
  <StackPanel Orientation=“Horizontal” />
 </ListBox.ItemsPanel>
</ListBox>
sky2008444 2012-11-22
  • 打赏
  • 举报
回复
你这里设置是横向滚动条自动显示 垂直滚动条始终隐藏啊
love_study 2012-11-22
  • 打赏
  • 举报
回复
请问 如何做啊,能贴下代码吗
Bullatus 2012-11-22
  • 打赏
  • 举报
回复
MouseWheel事件里调用滚动的方法就可以了
love_study 2012-11-21
  • 打赏
  • 举报
回复
没有人遇到这个问题吗?

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧