Search code examples
c#wpfxamlcontentcontrol

Adding WPF Page to WPF Window


I am making a common element like a button, so I made it in a WPF Page I designed it and I just want to know what the best way is to add that page in a WPF Window in the designer.

    <WrapPanel RenderTransformOrigin="0,0.5" Margin="0,0,754,0">
        
        
        <Button Content="" Height="36" Width="36" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" BorderThickness="0,0,0,0" RenderTransformOrigin="0.5,0.5"
                local:ButtonHelper.HoverImage="/Assets/Utility/menu_hovered.png">
            <Button.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform X="5" Y="5"/>
                </TransformGroup>
            </Button.RenderTransform>
            <Button.Background>
                <ImageBrush ImageSource="/Assets/Utility/menu.png"/>
            </Button.Background>
        </Button>
        
        
    </WrapPanel>
</Page>

this is just something I added in the WPF Page

I have tried adding Content Controller, but I can't understand it


Solution

  • <Grid x:Name="viewSpace">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Frame Name="mainFrm" Grid.Row="0" NavigationUIVisibility="Hidden"/>
    <!--only if you want to hide the nav bar keep it hidden-->
    </Grid>
    

    Setting the mainFrm.Content in your code behind to a page will change the content on the window.

    I would recommend not using a content control for WPF Pages. ContentControls work best with UserControls if I'm not mistaken.

    This is a good question found on stack overflow with some good answers.

    Window vs Page vs UserControl for WPF navigation?

    In the highest rated answer ContentArea.Content = new MyUserControl(); Is how they recommend Custom user control navigation with ContentControls. I don't know the ultimate answer what is best but I have always personally used pages for navigation since it works well with the MVVM structure.