2012-03-10 153 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(); 
    }