Search code examples
c#wpfxamluwp-xamlwinui-3

The page in WinUI3 cannot be filled automatically in TabViewItem


OrderV.xaml wrong effect desired effect

 Frame frame = new Frame();
 frame.Navigate(typeof(OrderV));

 TabViewItem tabViewItem = new()
 {
     Header = "Order",
     Content = frame
 };
 
tabViewItem.IsSelected = true;
TabViewControl.TabItems.Add(tabViewItem);

I've tried using Frame alone and it's fine, but I want Page to work in a TabViewItem

<Frame Name="MenuFrame" />
MenuFrame.Navigate(typeof(OrderV));

Solution

  • You need to add VerticalAlignment="Stretch" to your TabView control. Here is a minimal sample from your code.

    MainWindow.xaml

    <Grid RowDefinitions="Auto,*">
        <Button
            Grid.Row="0"
            Click="Button_Click"
            Content="Start" />
        <TabView
            x:Name="TabViewControl"
            VerticalAlignment="Stretch"
            Grid.Row="1" />
    </Grid>
    

    MainWindow.xaml.cs

    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Frame frame = new();
            frame.Navigate(typeof(OrderV));
    
            TabViewItem tabViewItem = new()
            {
                Header = "Order",
                Content = frame
            };
    
            tabViewItem.IsSelected = true;
            TabViewControl.TabItems.Add(tabViewItem);
        }
    }