2012-03-10 149 views
0

对于我正在处理的应用程序,我想创建一个自定义控件,它上面有几个按钮,如果有太多按钮,我需要它滚动。但是,我不想使用标准滚动条,而只是想在控件的两端有两个按钮向上滚动,而另一个向下滚动。达到此目的的最佳方法是什么?创建一个可滚动的WPF usercontrol

是否可以更改滚动条的样式,使它们只是按钮而不是完整的水平GUI?

回答

19

您通常会使用ScrollViewer来实现此目的。例如:

<UserControl x:Class="WpfApplication2.UserControl1" 
      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" 
      mc:Ignorable="d" 
      d:DesignHeight="200" d:DesignWidth="300"> 
    <ScrollViewer> 
     <StackPanel> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
      <Button Content="Test"/> 
     </StackPanel> 
    </ScrollViewer> 
</UserControl> 

在需要时会自动显示垂直滚动条。

您可以通过指定VerticalScrollbarVisibility这样

<ScrollViewer VerticalScrollBarVisibility="Auto"> 
<ScrollViewer VerticalScrollBarVisibility="Visible"> 
<ScrollViewer VerticalScrollBarVisibility="Hidden"> 
<ScrollViewer VerticalScrollBarVisibility="Disabled"> 

改变行为有当然是一个Horizo​​ntalScrollBarVisibility财产为好。

0

我会建议你使用标准的Windows滚动条,而不是使用一些自定义按钮来向上滚动和向下滚动。用户非常习惯于滚动目的。如果您使用两个按钮,则必须进一步显示滚动进度等其他内容。不要重新发明轮子:)

+0

我想使用两个按钮的唯一原因,是因为它是在触摸屏上米长跑,但我想我可以永远只是使滚动条触摸友好。 – user1145533 2012-03-24 08:09:20

0

解决此类问题的方法是检查控件的属性和方法。

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="auto" /> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="auto" /> 
    </Grid.RowDefinitions> 
    <Button Grid.Row="0" Width="40" HorizontalAlignment="Left" Content="Up" Click="clickSVup"/> 
    <ScrollViewer x:Name="svBtn" Grid.Row="1" Width="100" HorizontalAlignment="Left" 
        VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden"> 
     <StackPanel> 
      <TextBlock Text="Text 01"/> 
      <TextBlock Text="Text 02"/> 
      <TextBlock Text="Text 03"/> 
      <TextBlock Text="Text 04"/> 
      <TextBlock Text="Text 05"/> 
      <TextBlock Text="Text 06"/> 
      <TextBlock Text="Text 07"/> 
      <TextBlock Text="Text 08"/> 
      <TextBlock Text="Text 09"/> 
      <TextBlock Text="Text 10"/> 
      <TextBlock Text="Text 11"/> 
      <TextBlock Text="Text 12"/> 
      <TextBlock Text="Text 13"/> 
      <TextBlock Text="Text 14"/> 
      <TextBlock Text="Text 15"/> 
      <TextBlock Text="Text 16"/> 
      <TextBlock Text="Text 17"/> 
      <TextBlock Text="Text 18"/> 
      <TextBlock Text="Text 19"/> 
     </StackPanel> 
    </ScrollViewer> 
    <Button Grid.Row="2" Width="40" HorizontalAlignment="Left" Content="Down" Click="clickSVdn"/> 
</Grid> 



    private void clickSVdn(object sender, RoutedEventArgs e) 
    { 
     svBtn.PageDown(); 
    } 

    private void clickSVup(object sender, RoutedEventArgs e) 
    { 
     svBtn.PageUp(); 
    }