WPF菜单 - 动态显示和隐藏内容

时间:2016-03-30 11:56:29

标签: c# wpf xaml menu

我需要创建交互式菜单。选择选项时,我想显示适当的内容 例如,当单击选项“Schemat bazy Northwind”时,应该向我的网格添加Image。当选择其他选项时,之前的内容将被删除等。

我唯一想到的是创建功能,在开始时清除网格,然后添加内容(是否可能?)。

请有人指示我解决这个问题。

<Window x:Class="Northwind.AdminPanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Panel administratora" WindowState="Maximized">

    <StackPanel Name="bindingData">
        <StatusBar>
            <TextBlock FontSize="15" Text="{Binding ServerName}" Margin="0 0 30 0"></TextBlock>
            <TextBlock FontSize="15" Text="{Binding ConnectionStatus}" Margin="0 0 30 0"></TextBlock>
            <Label FontSize="15" Name="lblClock"></Label>
        </StatusBar>

        <DockPanel Height="55">
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="Baza" Margin="10" FontSize="15"></MenuItem>
                <MenuItem Header="Pomoc" Margin="10" FontSize="15">
                    <MenuItem x:Name="itemSchema" Header="Schemat bazy Northwind" Click="itmSchema_Click_1"></MenuItem>
                </MenuItem>
           </Menu>
        </DockPanel>

        <Grid x:Name="mainContent">  

          <!--add content -->                             

        </Grid>
</StackPanel>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace Northwind
{
    public partial class AdminPanel : Window
    {
        public string ServerName { get; set; }
        public string ConnectionStatus { get; set; }

        public AdminPanel(string name,string status)
        {
            InitializeComponent();

            this.ServerName = name;
            this.ConnectionStatus = status;

            DispatcherTimer dtClockTime = new DispatcherTimer();
            dtClockTime.Interval = new TimeSpan(0, 0, 1);
            dtClockTime.Tick += dtClockTime_Tick;

            dtClockTime.Start();
            bindingData.DataContext = this;
        }

        private void dtClockTime_Tick(object sender, EventArgs e)
        {
            lblClock.Content = DateTime.Now.ToLongTimeString();
        }

        private void itmSchema_Click_1(object sender, RoutedEventArgs e)
        {
           //code
        }
    }    
}    

2 个答案:

答案 0 :(得分:0)

您可以将菜单或任何您想要的项目放置到conatainer(网格),并且可以在要隐藏该控件时将Visibility属性用作折叠。 如果您没有使用MVVM模式,则将其添加到相应的事件中。

Container_Name.Visibility = Visibility.Collapsed;

否则您可以使用Xaml触发器获取相同的内容。 对于那个参考StackOverflow_Answer

答案 1 :(得分:0)

创建资源字典并添加此代码

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!--  Set names two different ways  -->
    <Button Name="okButton">OK</Button>
    <Button x:Name="cancelButton">Cancel</Button>
    <ListBox>
        <!--  Set content three different ways  -->
        <ListBoxItem Content="Item 1" />
        <ListBoxItem>Item 2</ListBoxItem>
        <ListBoxItem>
            <ListBoxItem.Content>Item 3</ListBoxItem.Content>
        </ListBoxItem>
    </ListBox> </StackPanel>

点击选项“Schemat bazy Northwind”时

private void SchematbazyNorthwind_Click(object sender, RoutedEventArgs e)
        {
            StackPanel stackPanel = null;
            using (FileStream fs =
            new FileStream("Dictionary1.xaml", FileMode.Open, FileAccess.Read))
            {
                stackPanel = (StackPanel)XamlReader.Load(fs);
            }
            MainGrid.Children.Add(stackPanel);
        }

加载资源并附加到网格