Search code examples
c#windowswindows-phone-8listboxmessagebox

Listbox in CustomMessageBox always scrolls up


I've got a weird problem with a listbox, which is placed in a custommessagebox.

Problem: You can't scroll down the list, because a "draggesture" up does the same thing than down. So the only thing I got is the respond-animation when your at the top of the list.

I made a little sample:

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>


    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
        <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <Button Background="Bisque" Content="Click Me" Click="Button_Click"></Button>

    </Grid>
</Grid>

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var source = new string[200];

        for (int i = 0; i < source.Length; i++)
        {
            source[i] = i.ToString();
        }

        var dialog = new CustomMessageBox()
        {
            Content = new ListBox(){ItemsSource = source},
            RightButtonContent = "Cancel",
            LeftButtonContent = "Store",
            VerticalContentAlignment = VerticalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center,
            Background = new SolidColorBrush(Colors.Black),
            HorizontalContentAlignment = HorizontalAlignment.Center,
            HorizontalAlignment = HorizontalAlignment.Center
        };

        dialog.Show();
    }
}

And here is the link, if you wanna download it: https://onedrive.live.com/redir?resid=6B27FD720F1FB58D!15988&authkey=!ACG5krcfzsoBxA8&ithint=folder%2csln

Does anyone has an idea whats going wrong? Thx for any help.


Solution

  • There is a simple fix for that.. Add a Height to the ListBox. Like this.

    var dialog = new CustomMessageBox()
    {
        Content = new ListBox(){ItemsSource = source, Height = 800},
        RightButtonContent = "Cancel",
        LeftButtonContent = "Store",
        VerticalContentAlignment = VerticalAlignment.Top, // Change to Top
        VerticalAlignment = VerticalAlignment.Top, // Change to Top
        Background = new SolidColorBrush(Colors.Black),
        HorizontalContentAlignment = HorizontalAlignment.Center,
        HorizontalAlignment = HorizontalAlignment.Center
    };
    

    Change the VerticalContentAlignment and VerticalAlignment to Top so when you set the Height value less than 800 the content is aligned to the top.