2016-12-21 124 views
1

所以我有這種情況下,我在ScrollViewer內顯示Grid。 我想說明沿着滾動條的組合框和圖像的方式,它不影響滾動功能, 事情是這樣的: enter image description here沿滾動條滾動查看器行顯示控件

目前只要ScrollViewer中變得可見它出現在一個新行,我怎麼能沿同一行中的控件顯示它?

這是我的XAML設計:

<DockPanel LastChildFill="True"> 

    <!--Top Panel--> 
    <Grid DockPanel.Dock="Top"> 
     --GridContent 
    </Grid> 

    <!--Bottom Panel--> 
    <Grid DockPanel.Dock="Bottom"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 


     <ComboBox Grid.Column ="0"> 
     </ComboBox> 

     <Image Grid.Column="1"> 

     </Image> 
    </Grid> 

    <ScrollViewer HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" 
        VerticalScrollBarVisibility="Auto" VerticalAlignment="Stretch" > 
     <Grid    
       HorizontalAlignment="Stretch" 
       VerticalAlignment="Stretch"> 
      -- Grid Content 
     </Grid> 
    </ScrollViewer> 
</DockPanel> 

目前看來是這樣的:

enter image description here

+0

除非你想要做一些相當複雜的自定義模板,快速的解決辦法只是隱藏在ScrollViewer中的horizo​​ntalbarvisibility,而是隻使用必然要與之交互的ScrollViewer中的偏移和scrollchanged性質滾動條。舉個例子比我在空閒時間需要更長的時間,但對不起amigo –

+0

在你的例子中,你的ScrollViewer和組合框和圖像不在同一個網格中。那是故意的嗎? – Bryan

+0

@Bryan是的,因爲我不希望這些控件在用戶滾動時與網格內容一起流動。 – Sameed

回答

2

我沒有看過在XAML這樣做,但你可以做這樣的在後面的代碼:

public partial class MainWindow : Window 
{ 
    private void IncrementColumn(UIElement element) 
    { 
     Grid.SetColumn(element, Grid.GetColumn(element) + 1); 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     scrollPanel.ApplyTemplate(); 

     var horizontal = scrollPanel.Template.FindName("PART_HorizontalScrollBar", scrollPanel) as ScrollBar; 
     var vertical = scrollPanel.Template.FindName("PART_VerticalScrollBar", scrollPanel) as ScrollBar; 
     var presenter = scrollPanel.Template.FindName("PART_ScrollContentPresenter", scrollPanel) as ScrollContentPresenter; 
     var corner = scrollPanel.Template.FindName("Corner", scrollPanel) as Rectangle; 
     var grid = corner.Parent as Grid; 

     grid.ColumnDefinitions.Insert(0, new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) }); 
     IncrementColumn(horizontal); 
     IncrementColumn(vertical); 
     IncrementColumn(corner); 
     Grid.SetColumnSpan(presenter, 2); 

     var panel = new StackPanel() { Orientation = Orientation.Horizontal }; 
     panel.Children.Add(new ComboBox()); 
     panel.Children.Add(new Image()); 

     Grid.SetRow(panel, 1); 
     Grid.SetColumn(panel, 0); 
     grid.Children.Add(panel); 
    } 
} 

這裏的XAML去用它:

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     Title="MainWindow" Height="150" Width="525"> 
     <ScrollViewer 
      Name="scrollPanel" 
      HorizontalScrollBarVisibility="Visible" 
      HorizontalAlignment="Stretch" 
      VerticalScrollBarVisibility="Auto" 
      VerticalAlignment="Stretch"> 
      <Grid    
       HorizontalAlignment="Stretch" 
       VerticalAlignment="Stretch"> 
       <Image Source="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"/> 
      </Grid> 
     </ScrollViewer> 
</Window> 
+0

感謝隊友,很好的回答,我遵循這個方法並在XAML中實現它,就像一個魅力。 – Sameed

+0

沒問題,我很高興它幫助你找到答案。我很想看到你的XAML解決方案。你可以把它添加到你的問題或其他? – sclarke81