Horizo​​ntal ListBox无法滚动Windows Phone 8.1 WP8.1

时间:2015-07-10 06:46:43

标签: c# wpf xaml windows-phone-8 listbox

我的WP8.1应用程序(WinRT)中遇到ListBox的问题,我无法使其水平滚动。 5个图像适合屏幕,6号后的所有内容都被简单地裁剪。 我尝试在列表框周围添加ScrollViewer ItemsPanelTemplate左右ListBox,但无效。 这是我的xaml代码

    <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中不存在此属性,并且在删除此属性并再次返回之后,我收到错误{ {1}}。 我试着自己搜索一个解决方案,看看这里,谷歌搜索但我找不到解决方案。有人可以帮我弄这个吗?我用它破了2天。

更新:

列表框位于网格内,具有以下设置:

Syntax Error found in XBF generation

第二行高度设置为auto,因为我正在为列表框高度设置动画。列表框充当AppBar,当我按下应用栏上的按钮时,列表框显示包含菜单项。

解决方案更新:

受到SWilko提供的解决方案的启发,我还设法通过我的'ListBox'的更多配置来解决我的问题,因此修复我之前的代码如下:

    <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>

所有应该做的就是禁用垂直滚动并启用水平滚动。

1 个答案:

答案 0 :(得分: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被包围。 希望你能适应你的代码