Joakim Granaas
Joakim Granaas

Reputation: 87

Why is my SplitView's Content and Pane on the same area?

I've been searching and looking for examples all day and I cannot find the solution for my problem. I have a SplitView with a hamburger menu in it's Pane, and I am loading a frame for each of the ListBoxItems selected. But when my page loads, it looks something like this(the orange area is the page background): Start page I also wonder how I can set any of my frames to be loaded at startup, I've tried to set TodayListBoxItem to IsSelected="true" and IsEnabled="true" but all I get is the same startup page as shown above, but the TodayListBoxItem is selected. Here's my XAML:

<Page
x:Class="Hamburger.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Hamburger"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <RelativePanel>
        <Button Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content="&#xE700;" FontSize="36" Click="HamburgerButton_Click" />
        <TextBlock Name="PageTitle" FontSize="30" Margin="70,0,0,0"></TextBlock>
    </RelativePanel>
    <SplitView Name="MySplitView" 
               Grid.Row="1" 
               DisplayMode="CompactInline" 
               OpenPaneLength="200" 
               CompactPaneLength="56" 
               HorizontalAlignment="Left">
        <SplitView.Pane>
            <ListBox SelectionMode="Single" 
                     Name="IconsListBox"
                     SelectionChanged="IconsListBox_SelectionChanged">
                <ListBoxItem Name="TodayListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE909;" />
                        <TextBlock Text="Today" FontSize="24" Margin="20,0,0,0" />
                    </StackPanel>
                </ListBoxItem>

                <ListBoxItem Name="ForecastListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE8F5;" />
                        <TextBlock Text="Forecast" FontSize="24" Margin="20,0,0,0" />
                    </StackPanel>
                </ListBoxItem>

                <ListBoxItem Name="SettingsListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE713;" />
                        <TextBlock Text="Settings" FontSize="24" Margin="20,0,0,0" />
                    </StackPanel>
                </ListBoxItem>

            </ListBox>
        </SplitView.Pane>
        <SplitView.Content >
            <Frame Name="ContentFrame" Background="Tomato"  HorizontalAlignment="Stretch" />
        </SplitView.Content>
    </SplitView>
</Grid>
</Page>

And here's my xaml.cs:

namespace Hamburger
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }
    private void HamburgerButton_Click(object sender, RoutedEventArgs e)
    {
        MySplitView.IsPaneOpen = !MySplitView.IsPaneOpen;
    }

    private void IconsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (TodayListBoxItem.IsSelected)
        {
            PageTitle.Text = "Today";
            ContentFrame.Navigate(typeof(TodayPage));
/*
            Trying to use a method in TodayPage so that it shows the current
            weather
*/
        }
        else if (ForecastListBoxItem.IsSelected)
        {
            PageTitle.Text = "Forecast";
            ContentFrame.Navigate(typeof(ForecastPage));
        }
        else if (SettingsListBoxItem.IsSelected)
        {
            PageTitle.Text = "Settings";
            ContentFrame.Navigate(typeof(SettingsPage));
        }
    }
}
}

Upvotes: 3

Views: 88

Answers (1)

Bart
Bart

Reputation: 10015

The problem is within the definition of your SplitView.

<SplitView Name="MySplitView" 
           Grid.Row="1" 
           DisplayMode="CompactInline" 
           OpenPaneLength="200" 
           CompactPaneLength="56" 
           HorizontalAlignment="Left">

Since you're using HorizontalAlignment="Left", you're telling the SplitView "only take the width you need to fit my content". As there is initially no content, it will size to a width of 200px as that's the width it needs for the OpenPaneLength.

Simply remove the HorizontalAlignment (as it is Stretch by default).

<SplitView Name="MySplitView" 
           Grid.Row="1" 
           DisplayMode="CompactInline" 
           OpenPaneLength="200" 
           CompactPaneLength="56">

Bonus: if you want to fake a selection changed event to load a page on the initial load, there are several options. One of them is to select an item after the page is loaded:

    public MainPage()
    {
        this.InitializeComponent();
        this.Loaded += (sender, args) => IconsListBox.SelectedItem = IconsListBox.Items.First();
    }

Upvotes: 1

Related Questions