2017-08-21 42 views
1

我有一個的ScrollViewer內以下的StackPanel,顯示用戶控制元件每當一個特定事件發生:XAML/WPF - ScrollViewer中擁有的StackPanel內部沒有滾動

注:許多用戶控件可能會出現在StackPanel中那爲什麼我添加了一個ScrollViewer中

<ScrollViewer 
     VerticalScrollBarVisibility="Auto" 
     Grid.Row="2" 
     CanContentScroll="True" 
     Grid.ColumnSpan="2"> 

     <StackPanel Orientation="Vertical"> 
      <ItemsControl ItemsSource="{Binding UserControls}"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <views:UserControl/> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </StackPanel> 
</ScrollViewer> 

雖然,該StackPanel中仍是走出去的範圍和滾動條不顯示並不起作用!

我試圖修復雙方的StackPanel和ItemsControl的高度,但它似乎簡化版,要麼工作...含的ScrollViewer

窗口布局:

<Grid Margin="0,15,0,0"> 

    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 

    <Label 
     Content="This is a Label" 
     HorizontalAlignment="Left" 
     VerticalAlignment="Top" 
     Margin="5,5,0,0" 
     FontSize="15" 
     Grid.Row="0" Grid.ColumnSpan="2"> 
    </Label> 


    <StackPanel Grid.Row="1" Orientation="Horizontal" Grid.ColumnSpan="2"> 
     <ComboBox 
      ItemsSource="{Binding Something}" 
      Text="Confirm with..." 
      SelectedItem="{Binding Something}"/> 
     <Button 
      HorizontalAlignment="Left" 
      Margin="5" 
      Content="Add new UserControl" 
      Command="{Binding Path=AddUserControl}"/> 

    </StackPanel> 

    <ScrollViewer 
     VerticalScrollBarVisibility="Auto" 
     Grid.Row="2" 
     CanContentScroll="True" 
     HorizontalScrollBarVisibility="Auto"> 
     <StackPanel Orientation="Vertical"> 
      <ItemsControl ItemsSource="{Binding UserControls}" Height="300"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <views:UserControl/> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </StackPanel> 
    </ScrollViewer> 

</Grid> 

這裏是我的被添加到StackPanel中的ScrollViewer中內部用戶控件:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 

    <StackPanel 
     Orientation="Horizontal" 
     Grid.Row="0"> 

     <Button 
      Name="DeleteFilter" 
      HorizontalAlignment="Left" 
      Margin="5" 
      Content="-"/> 

     <ComboBox 
      Margin="5" 
      IsEditable="False" 
      IsReadOnly="True" 
      Width="150" 
      ItemsSource="{Binding SomeObject}" 
      DisplayMemberPath="Name" 
      SelectedItem="{Binding SomeObjectProperty}"/> 

     <ComboBox 
      Margin="5" 
      IsEditable="False" 
      IsReadOnly="True" 
      Width="150" 
      ItemsSource="{Binding AnotherObject}" 
      DisplayMemberPath="Name" 
      SelectedItem="{Binding AnotherObjectProperty}"/> 

     <TextBox 
      x:Name="Value" 
      Text="{Binding TextBoxValueString}" 
      TextAlignment="Center" 
      Width="100" 
      Margin="5" 
      Visibility="{Binding TextBoxVisibility}"/> 

    </StackPanel> 

</Grid> 

我是XAML和WPF的新手。

有什麼建議嗎?

+0

能否請你告訴整個佈局代碼的參考滾動查看器擁有哪些父母以及設置了哪些屬性? –

+0

您的代碼在我的機器上工作正常,您是否有機會在應用程序中的某處覆蓋Scroll Viewer的風格? –

+0

那很奇怪。不,不是。我不是壓倒性的。 –

回答

1

ScrollViewersStackPanel不能很好地協同工作。這是因爲StackPanel如果它的Orientation屬性設置爲Horizontal,並且設置爲Vertical的無限垂直空間,則其子元素具有無限水平空間。請參考我的答案這裏瞭解更多信息:

How to scroll the datagrid in stackpanel?

所以,你應該更換StackPanel另一個Panel或者乾脆刪除它:

<ScrollViewer 
     VerticalScrollBarVisibility="Auto" 
     Grid.Row="2" 
     CanContentScroll="True" 
     HorizontalScrollBarVisibility="Auto"> 
     <ItemsControl ItemsSource="{Binding UserControls}" Height="300"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <views:UserControl/> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
</ScrollViewer> 
+0

親愛的@ mm8,這是你第二次回答我的stackoverflow問題,並且你釘了它兩次:) ...謝謝很多。完美的作品! –

+1

我做過了,但是我只有10個聲望,我需要5才能夠投票。既然你投了我的問題,我現在可以做到了:) –

-1

您缺少StackPanel或ItemsControl(您的選擇)的背景。 默認背景爲空。 使用Background Null,ScrollViewer不會獲得鼠標滾輪的鼠標事件, 並且不滾動。

+0

你的意思是這樣的: Background =「LightGray」...如果你這樣做,那麼不,它沒有工作。面板仍然離開窗口的範圍 –

+0

背景與滾動查看器的可見性無關 –

相關問題