2010-06-04 53 views
5

我強調'任何'的原因是因爲CanContentScroll在我的ScollViewer中沒有完全工作。 讓我解釋一下場景: 我有一個ScrollViewer,它有三個標籤,每個標籤後面跟着一個ListBox。我在ScrollViewer裏有這個內容的原因是因爲我不希望每個ListBox都有一個ScrollBar,我只想要一個「全局」ScrollBar。問題是,當光標位於ListBox上時,ScrollViewer不滾動。我試圖在ScrollViewer,ListBox和ListBoxItem樣式中將CanContentScroll屬性設置爲true,但沒有成功。是否有其他控制類型我應該使用? 這裏是我的代碼示例:如何使ScrollViewer在鼠標結束時滾動* any * content

<UserControl x:Class="Telbit.TeStudio.View.Controls.TestStepsView" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:my="clr-namespace:Telbit.TeStudio.View.Controls"> 

<UserControl.Resources> 
    <DataTemplate DataType="{x:Type my:TestStepsStepViewModel}"> 
     <my:TestStepsStepView HorizontalAlignment="Stretch"/> 
    </DataTemplate> 

    <Style x:Key="StepItemStyle" TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="SnapsToDevicePixels" Value="true"/> 
     <Setter Property="OverridesDefaultStyle" Value="true"/> 
     <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/> 
     <Setter Property="ScrollViewer.CanContentScroll" Value="True"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBoxItem"> 
        <Border Name="Border" SnapsToDevicePixels="true" Background="Transparent" BorderThickness="0" Padding="1"> 
         <ContentPresenter/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsSelected" Value="true"> 
          <Setter TargetName="Border" Property="Background" Value="#40a0f5ff"/> 
         </Trigger> 
         <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
          <Setter Property="IsSelected" Value="True" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 

<UserControl.Background> 
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
     <GradientStop Color="#FFF2F2F2"/> 
     <GradientStop Color="Gainsboro" Offset="1"/> 
    </LinearGradientBrush> 
</UserControl.Background> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="30"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <HeaderedContentControl Grid.Row="0" > 
     <HeaderedContentControl.Header> 
      <Grid Background="#e8f2f8"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="30"/> 
        <ColumnDefinition MinWidth="200" Width="*" /> 
        <ColumnDefinition Width="75"/> 
        <ColumnDefinition Width="60"/> 
        <ColumnDefinition Width="60"/> 
        <ColumnDefinition Width="60"/> 
        <ColumnDefinition Width="60"/> 
        <ColumnDefinition Width="120"/> 
        <ColumnDefinition Width="130"/> 
       </Grid.ColumnDefinitions> 

       <Label Grid.Column="0" Content="#" BorderBrush="#70add4" BorderThickness="2 2 0 2"/> 
       <Label Grid.Column="1" 
         Content="Folder\Name" 
         BorderBrush="#70add4" BorderThickness="0 2 0 2"/> 
       <Label Grid.Column="2" Content="Type" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/> 
       <Label Grid.Column="3" Content="Auto Start" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/> 
       <Label Grid.Column="4" Content="Run After" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/> 
       <Label Grid.Column="5" Content="Stop After" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/> 
       <Label Grid.Column="6" Content="Delay (s)" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/> 
       <Label Grid.Column="7" Content="Timestamp" BorderBrush="#70add4" BorderThickness="0 2 0 2" Margin="-20 0 0 0"/> 
       <Label Grid.Column="8" Content="Edited by" BorderBrush="#70add4" BorderThickness="0 2 2 2" Margin="-20 0 0 0"/> 
      </Grid> 
     </HeaderedContentControl.Header> 
    </HeaderedContentControl> 
    <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1" VerticalAlignment="Top" CanContentScroll="True"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="30"/> 
       <RowDefinition Height="3*"/> 
       <RowDefinition Height="30"/> 
       <RowDefinition Height="3*"/> 
       <RowDefinition Height="30"/> 
       <RowDefinition Height="3*"/> 
      </Grid.RowDefinitions> 

      <Label Name="lblSetup" Grid.Row="0" 
        VerticalContentAlignment="Center" 
        BorderBrush="DarkGray" BorderThickness="0 0 0 1" 
        TextBlock.FontSize="10pt" TextBlock.FontWeight="Bold" TextBlock.Foreground="#949494" 
        Content="Setup" AllowDrop="True"/> 
      <ListBox Name="itmCtrlSetupSteps" Grid.Row="1" 
        BorderThickness="0" Background="Transparent" 
        ItemsSource="{Binding SetupSteps}" SelectionMode="Single" 
        HorizontalContentAlignment="Stretch" 
        ItemContainerStyle="{StaticResource StepItemStyle}" 
        SelectionChanged="manageStep_SelectionChanged" 
        ScrollViewer.CanContentScroll="True" 
        /> 

      <Label Name="lblTest" Grid.Row="2" 
        VerticalContentAlignment="Center" 
        BorderBrush="DarkGray" BorderThickness="0 0 0 1" 
        TextBlock.FontSize="10pt" TextBlock.FontWeight="Bold" TextBlock.Foreground="#949494" 
        Content="Test" AllowDrop="True"/> 
      <ListBox Name="itmCtrlTestSteps" Grid.Row="3" 
        BorderThickness="0" Background="Transparent" 
        ItemsSource="{Binding TestSteps}" SelectionMode="Single" 
        HorizontalContentAlignment="Stretch" 
        ItemContainerStyle="{StaticResource StepItemStyle}" 
        SelectionChanged="manageStep_SelectionChanged" 
        /> 

      <Label Name="lblTearDown" Grid.Row="4" 
        VerticalContentAlignment="Center" 
        BorderBrush="DarkGray" BorderThickness="0 0 0 1" 
        TextBlock.FontSize="10pt" TextBlock.FontWeight="Bold" TextBlock.Foreground="#949494" 
        Content="Tear Down" AllowDrop="True"/> 
      <ListBox Name="itmCtrlTearDownSteps" Grid.Row="5" 
        BorderThickness="0" Background="Transparent" 
        ItemsSource="{Binding TearDownSteps}" SelectionMode="Single" 
        HorizontalContentAlignment="Stretch" 
        ItemContainerStyle="{StaticResource StepItemStyle}" 
        SelectionChanged="manageStep_SelectionChanged" 
        /> 
     </Grid> 
    </ScrollViewer> 
</Grid> 
</UserControl> 

回答

11

的問題是,即使孩子列表Boxen有沒有滾動條可見,他們在他們ScrollViewer根據自己的模板。幸運的是,這個模板很容易修改。這樣做對每個孩子列表框中,或更好,但把它放在常見的款式:

<ListBox.Template> 
    <ControlTemplate TargetType="ListBox"> 
     <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderBrush}"> 
      <ItemsPresenter/> 
     </Border> 
    </ControlTemplate> 
</ListBox.Template> 

默認幾乎與周圍ItemsPresenterScrollViewer包裝除外相同。

+0

事情是我不希望每個ListBox有一個ScrollViewer(我有3個)。我想要一個'全局的''ScrollViewer',它給人一種獨特的'ListBox'的感覺。從我的理解你的代碼會給我一個'ScrollViewer'每個ListBox',對吧? – jpsstavares 2010-06-07 09:00:07

+0

不,我的代碼中沒有ScrollViewer。這就是整個問題。將其應用於兒童以擺脫其中的滾動。 – repka 2010-06-07 13:44:38

+0

對不起,懷疑你,我應該先試過解決方案.. 它的工作,非常感謝你! – jpsstavares 2010-06-08 09:00:32