简体   繁体   English

如何在WPF中制作动态主页内容

[英]How to make dynamic main page content in WPF

在此处输入图片说明

I want to make change main panel content when I choice action in menu button. 我想在菜单按钮中选择动作时更改主面板的内容。 Like main content page, setting page, content page on Main panel (in code used grid x:Name="main_~~~) It for use just can make 3 and control visibility?? or Can use include like xml at Android and change include target?? ( ex > includelayout="@layout/app_bar_main") 像主要内容页面,设置页面,主面板上的内容页面一样(在使用代码的网格x:Name =“ main_ ~~~)中使用只能使3和控制可见性??或可以在Android中使用诸如xml之类的内容并进行更改包括目标??(例如> includelayout =“ @ layout / app_bar_main”)

Mainwindow.xaml 主窗口

<Window x:Class="M_C.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-
compatibility/2006"
        xmlns:local="clr-namespace:M_C"
        mc:Ignorable="d"
        Title="MainWindow"
        Width="1024" Height="768"
        WindowStyle="None"
        WindowState="Maximized"
        WindowStartupLocation="CenterScreen">
    <Grid x:Name="wide_Out" Background="#000000">
        <Viewbox Stretch="Uniform">
            <Grid Width="1024" Height="768" VerticalAlignment="Top">
                <DockPanel x:Name="L_black" HorizontalAlignment="Left" 
Height="768" LastChildFill="False" VerticalAlignment="Top" Width="62" 
Background="#FF242424">
                    <Button Margin="15,8,0,0" Height="40" 
VerticalAlignment="Top" Width="30"/>
                </DockPanel>
                <DockPanel x:Name="T_blue" HorizontalAlignment="Left" 
Height="84" LastChildFill="False" Margin="62,0,0,0" VerticalAlignment="Top" 
Width="968" Background="#FF248BC7">
                    <TextBlock Margin="200,5,200,0" Height="70" 
TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="554"/>
                </DockPanel>
                <DockPanel x:Name="L_blue" HorizontalAlignment="Left" 
Height="685" LastChildFill="False" Margin="62,83,0,0" 
VerticalAlignment="Top" Width="82" Background="#FF248BC7"/>
                <DockPanel x:Name="R_blue" HorizontalAlignment="Left" 
Height="685" LastChildFill="False" Margin="940,83,0,0" 
Background="#FF248BC7" Width="84"/>
                <DockPanel x:Name="B_blue" HorizontalAlignment="Left" 
Height="90" LastChildFill="False" Margin="143,678,0,0" 
VerticalAlignment="Top" Width="799" Background="#FF248BC7">
                    <Image Margin="250,15,250,15" Height="70" 
VerticalAlignment="Top" Width="290" />
                </DockPanel>
                <!--<DockPanel x:Name="main_content_panel" 
HorizontalAlignment="Left" Height="594" LastChildFill="False" 
Margin="144,84,0,0" VerticalAlignment="Top" Width="790">-->
                <Grid x:Name="main_con_body" Margin="143,82,84,88" 
Width="798" Height="594" Background="#ffffff">
                </Grid>
            <!--</DockPanel>-->
         </Grid>
     </Viewbox>
   </Grid>
</Window>

You can Define Frame inside Your Window and navigate between as many page you want: 您可以在“窗口”内定义框架并在所需的多个页面之间导航:

<Frame Name="FrameWithinGrid" >  
</Frame>  

and on button Click you can simply navigate: 然后单击按钮,您可以轻松导航:

private void button1_Click(object sender, RoutedEventArgs e)  
{  
   FrameWithinGrid.Navigate(new System.Uri("Page1.xaml",  
         UriKind.RelativeOrAbsolute));  
} 

to know more See here 了解更多请看这里

MVVM Approach MVVM方法

  1. Make main_con_body be a ContentControl 使main_con_body成为ContentControl
  2. Make Window's DataContext a viewmodel 使Window的DataContext成为视图模型
  3. Bind main_con_body's Content property to a viewmodel property of the data context 将main_con_body的Content属性绑定到数据上下文的viewmodel属性
  4. Define different DataTemplates for the various types of viewmodels that might be present 为可能存在的各种类型的视图模型定义不同的数据模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM