【问题标题】:WPF menu - Dynamic show and hide contentWPF 菜单 - 动态显示和隐藏内容
【发布时间】:2016-07-18 09:57:20
【问题描述】:

我需要创建交互式菜单。选择选项后,我想显示适当的内容。
例如,当单击“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
        }
    }    
}    

【问题讨论】:

    标签: c# wpf xaml menu


    【解决方案1】:

    您可以将菜单或您想要的任何项目放入容器(网格)中,当您想要隐藏该控件时,您可以使用折叠状态的 Visibility 属性。 如果您没有使用 MVVM 模式,请将其添加到相应的事件中。

    Container_Name.Visibility = Visibility.Collapsed;
    

    否则,您可以使用 Xaml 触发器来获得相同的结果。 为此请参考StackOverflow_Answer

    【讨论】:

    • 感谢您的回答。您的解决方案非常简单,而且是我想要的。我为我的 XAML 代码创建了新容器,并将它们设置为属性 Visbility 和 Collapsed 值。也许我的问题会很愚蠢,但是对于 HTML 中的示例,我们可以在元素上设置类和 id。在 XAML 中,我们只能使用一次 Name 属性,并且在后面的代码中我必须编写所有容器 Name 来设置 Collapsed 值。这不是问题,但也许可以改进一点。
    • 我建议你全部在 MVVM 模式中完成。这更容易。
    • 我尝试使用 MVVM 模式。我在 wpf 中阅读了一些关于资源字典的信息,但不幸的是,我遇到了关联键属性的问题。我知道使用 MVVM 更专业,但现在使用属性 Visible 对我来说更容易。当然,将来我会注意您的解决方案。谢谢你的回答。
    【解决方案2】:

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

    <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);
            }
    

    加载您的资源并附加到您的网格

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2012-08-24
      • 2011-10-27
      • 1970-01-01
      • 2016-04-14
      相关资源
      最近更新 更多