【问题标题】:How to make dynamic main page content in WPF如何在 WPF 中制作动态主页内容
【发布时间】:2018-04-28 17:08:04
【问题描述】:

我想在菜单按钮中选择操作时更改主面板内容。 像主内容页面,设置页面,主面板上的内容页面(在代码中使用网格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>

【问题讨论】:

  • "It for use just can make 3 and control visibility??"是什么意思?我几乎无法弄清楚问题的其余部分,但那个问题让我很难过。

标签: c# wpf layout


【解决方案1】:

您可以在您的窗口内定义框架并在您想要的任意数量的页面之间导航:

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

点击按钮即可轻松导航:

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

了解更多See here

【讨论】:

    【解决方案2】:

    MVVM 方法

    1. 将 main_con_body 设为 ContentControl
    2. 使 Window 的 DataContext 成为视图模型
    3. 将 main_con_body 的 Content 属性绑定到数据上下文的 viewmodel 属性
    4. 为可能存在的各种类型的视图模型定义不同的 DataTemplates

    【讨论】:

    • 感谢您的回答。我稍后再练习。
    猜你喜欢
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 2017-06-05
    • 2013-12-23
    • 1970-01-01
    相关资源
    最近更新 更多