2015-07-10 61 views
2

我遇到了我的WP8.1的應用程序(WinRT的)ListBox一個問題,我無法使其水平滾動。 5張圖片適合屏幕,6張以後的所有內容都是簡單的裁剪。 我試着在列表框的周圍添加ScrollViewerItemsPanelTemplate的地址爲ListBox,沒有任何效果。 這是我的XAML代碼水平列表框無法滾動的Windows Phone 8.1 WP8.1

<ListBox x:Name="AppBarMenu" 
     Grid.Row="1" 
     Canvas.ZIndex="1" 
     ScrollViewer.HorizontalScrollBarVisibility="Auto" 
     Background="{StaticResource BackgroundColorApp}" 
     ItemTemplate="{StaticResource StackMenuItem}" 
     ItemsSource="{Binding}" 
     Style="{StaticResource ListBoxHorizontal}" 
     ItemContainerStyle="{StaticResource ListBoxContainerStylePP}" 
     Foreground="{StaticResource TBColorNonSelected}" 
     SelectedIndex="{Binding SelectedIndex, ElementName=PetProtectorFrames, Mode=TwoWay}" 
     Height="0" 
     VerticalAlignment="Top" 
     SelectionChanged="AppBarMenu_SelectionChanged" 
     ScrollViewer.VerticalScrollBarVisibility="Disabled"> 

    </ListBox> 

這是ItemsPanelTemplate

<Style x:Key="ListBoxHorizontal" 
     TargetType="ListBox"> 
    <Setter Property="BorderThickness" 
      Value="0" /> 
    <Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal" 
          VerticalAlignment="Center" 
          HorizontalAlignment="Center" /> 
      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

我事件模板嘗試添加VirtualizationStackpanel而不是堆面板ItemsPanemTemplate但它的作用是相同的。當我嘗試設置屬性CanHorizontallyScroll=true,我得到兩個錯誤,第一是這個屬性不裏VirtualizationStackpanel存在,刪除這個屬性,又折回來之後,我得到的錯誤Syntax Error found in XBF generation。 我試着自己尋找一個解決方案,看這裏,谷歌搜索它,但我找不到解決方案。有人可以幫我弄這個嗎?我已經連續2天都沒有理會我的頭了。

UPDATE:

列表框裏面網格如下因素設置:

<Grid x:Name="MainGrid"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="0.091*" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="0.01*" /> 
     <RowDefinition Height="0.9*" /> 

    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 

,因爲我的動畫列表框高度第二行的高度設置爲自動。 Listbox充當AppBar,當我按下應用欄上的按鈕時,列表框顯示出包含菜單項。

SOLUTION UPDATE:

由SWilko提供的解決方案的啓發,我也設法解決我的問題,我的'ListBox的,所以修復到我以前的代碼較少的配置是這樣的:

<ListBox x:Name="AppBarMenu" 
    Grid.Row="1" 
    Canvas.ZIndex="1" 
    ScrollViewer.HorizontalScrollMode="Enabled" 
    ScrollViewer.HorizontalScrollBarVisibility="Visible" 
    ScrollViewer.VerticalScrollBarVisibility="Disabled" 
    ScrollViewer.VerticalScrollMode="Disabled" 
    Background="{StaticResource BackgroundColorApp}" 
    ItemTemplate="{StaticResource StackMenuItem}" 
    ItemsSource="{Binding}" 
    Style="{StaticResource ListBoxHorizontal}" 
    ItemContainerStyle="{StaticResource ListBoxContainerStylePP}" 
    Foreground="{StaticResource TBColorNonSelected}" 
    SelectedIndex="{Binding SelectedIndex, ElementName=PetProtectorFrames, Mode=TwoWay}" 
    Height="0" 
    VerticalAlignment="Top" 
    SelectionChanged="AppBarMenu_SelectionChanged"> 

</ListBox> 

所有應該做的就是禁用垂直滾動並啓用水平滾動。

+0

你可以張貼的XAML即'Grid'或面板周圍的'ListBox''休息嗎? – SWilko

+0

我更新了問題 – Shakal187

回答

0

的第一件事是你的ListBox的高度設置爲0,但推測,可能是筆誤:)

這裏是ListBox一個簡單的例子水平滾動。

Item.cs

public class Item 
{ 
    public string Name { get; set; } 
} 

MainPage.xaml.cs中構造

public MainPage() 
    { 
     this.InitializeComponent(); 

     this.NavigationCacheMode = NavigationCacheMode.Required; 

     var list = new List<Item> 
     { 
      new Item { Name = "Item 1" }, 
      new Item { Name = "Item 2" }, 
      new Item { Name = "Item 3" }, 
      new Item { Name = "Item 4" }, 
      new Item { Name = "Item 5" }, 
      new Item { Name = "Item 6" }, 
      new Item { Name = "Item 7" }, 
      new Item { Name = "Item 8" } 
     }; 

     this.AppBarMenu.ItemsSource = list; 
    } 

MainPage.xaml中

<Grid> 
    <ScrollViewer 
    ScrollViewer.HorizontalScrollMode="Enabled" 
    ScrollViewer.HorizontalScrollBarVisibility="Visible" 
    ScrollViewer.VerticalScrollBarVisibility="Disabled" 
    ScrollViewer.VerticalScrollMode="Disabled"> 
     <ListBox x:Name="AppBarMenu" 
      ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
      ScrollViewer.VerticalScrollBarVisibility="Disabled" 
       Height="100" 
      VerticalAlignment="Top"> 
      <ListBox.ItemsPanel> 
       <ItemsPanelTemplate> 
        <StackPanel Orientation="Horizontal"/> 
       </ItemsPanelTemplate> 
      </ListBox.ItemsPanel> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding Name}" Foreground="Red" 
          FontSize="30"/> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </ScrollViewer> 
</Grid> 

ListBox ScrollViewer中被禁用,ScrollViewer被纏着。 希望你能適應你的代碼

+0

嗨,我會嘗試它,當我回家時,我會發布結果:)從這一點上,我會說它可能工作,因爲我從來沒有試圖禁用列表框內的ScrollViewer ...而且,它不是拼寫錯誤,首次載入時我需要將菜單(列表框)「隱藏」。所以當我按下按鈕時,它會顯示出動畫:)謝謝! :) – Shakal187

+0

@ Shakal187是由於不可預知的行爲,使用包裝'ScrollViewer'而不是'ListBox'是一個很常見的過程。祝你好運 – SWilko

+0

我試過你的解決方案,它像一個魅力:)非常感謝你!正如你可以預測的那樣,我仍然是wp8.1編程中的一名新手,但我正努力成爲wp8.1的一名經驗豐富的開發人員。此外,我試圖「設置」列表框的'ScrollViewer'就像你用包裝'ScrollViewer'一樣。對於那些遇到這個問題的人,我也會更新我的問題。再一次,非常感謝! – Shakal187